0

誰かが私を正しい方向に向けてくれることを願っています。私は nHibernate を使い始めたばかりで、これについて少し混乱しています。.Net Web アプリケーション内で実行されています。

基本的に、クーポンとパブリッシャーの 2 つのクラスがあります。

テストとして、NH が正しく設定されていることを確認するために、PublisherRepository にアクセスし、その名前で発行元を引き出しました。それはうまく機能し、成功を報告します。

IPublisherRepository repo = new PublisherRepository();
Response.Write(repo.GetByName("Publisher 5"));

2 番目のテストとして、次のように CreateQuery メソッドを使用してすべての Publisher を取得しました。

IQuery query = session.CreateQuery("from CartManData.Domain.Publisher pub");

これはデータを返しません - リストは空です。Linq を使用しても同じことが言えます。

session.Query<Publisher>().Where(x=>x.Name == "Publisher 4").ToList<Publisher>()

Sql Profiler を使用すると、最初のテストがデータベースにヒットし、遅延読み込みがオフになっているため、セット (パブリッシャに属するクーポンと呼ばれる) が取得されることがわかります。ただし、2 番目の 2 つのアプローチはデータベースにまったくヒットしません。その理由がわかりません。

Publisher と Coupon のマッピング ファイルは次のとおりです。それらは埋め込まれており、機能していることはわかっています。そうしないと、レポも機能しません。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="CartManData"
                   namespace="CartManData.Domain">

  <class name="Publisher" lazy="false">
    <id name="Id">
      <generator class="guid"></generator>
    </id>
    <property name="Name"></property>
    <property name="AddressLine1"></property>
    <property name="AddressLine2"></property>
    <property name="AddressLine3"></property>
    <property name="Town"></property>
    <property name="PostCode"></property>
    <property name="Telephone"></property>
    <property name="Email"></property>
    <property name="Enabled"></property>
    <property name="CommissionRate"></property>
    <set name="Coupons" cascade="none" lazy="false">
      <key column="PublisherId" ></key>
      <one-to-many class="Coupon" />
    </set>
  </class>
</hibernate-mapping>

そしてクーポン:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="CartManData"
                   namespace="CartManData.Domain">

  <class name="Coupon">
    <id name="Id">
      <generator class="guid"></generator>
    </id>
    <property name="Name"></property>
    <property name="EffectiveFrom"></property>
    <property name="EffectiveTo"></property>
    <property name="UnitPrice"></property>
    <property name="OriginalPrice"></property>
    <property name="CouponImage"></property>
    <property name="Enabled"></property>
    <property name="PublisherId" not-null="false"></property>
  </class>
</hibernate-mapping>

これに関するヘルプは本当に感謝しています-確かに私が見逃したものです。

乾杯、トニー

追加情報

Session オブジェクトは、セッション オブジェクトが CurrentSessionContext にバインドされている HttpModule を介して取得されます。セッションが開いているかどうかを確認すると、それが報告されているように、それはうまく機能しているようです。

PublisherRepository.GetByName() は次のようになります。

using (ISession session = NHibernateHelper.OpenSession())
            {
                return session.CreateCriteria(typeof(Publisher))
                    .Add(NHibernate.Criterion.Restrictions.Eq("Name", name))
                    .UniqueResult<Publisher>();
            }

Log4Net 出力

CreateQuery による呼び出し中 (上記の 2 番目の例)、NHibernate は次のように報告しています。

2012-08-22 16:22:28,075 [15] DEBUG rollingFile - START of retrieval
2012-08-22 16:22:28,081 [15] DEBUG NHibernate.Engine.Query.QueryPlanCache - unable to locate HQL query plan in cache; generating (from CartManData.Domain.Publisher pub)
2012-08-22 16:22:28,128 [15] DEBUG NHibernate.Hql.Ast.ANTLR.HqlParseEngine - parse() - HQL: from CartManData.Domain.Publisher pub
2012-08-22 16:22:28,174 [15] DEBUG NHibernate.Hql.Ast.ANTLR.ErrorCounter - throwQueryException() : no errors
2012-08-22 16:22:28,200 [15] DEBUG NHibernate.Engine.Query.QueryPlanCache - unable to locate HQL query plan in cache; generating (from CartManData.Domain.Publisher pub)
2012-08-22 16:22:28,201 [15] DEBUG NHibernate.Hql.Ast.ANTLR.HqlParseEngine - parse() - HQL: from CartManData.Domain.Publisher pub
2012-08-22 16:22:28,202 [15] DEBUG NHibernate.Hql.Ast.ANTLR.ErrorCounter - throwQueryException() : no errors
2012-08-22 16:22:28,206 [15] DEBUG NHibernate.Engine.Query.HQLQueryPlan - enumerable: from CartManData.Domain.Publisher pub
2012-08-22 16:22:28,208 [15] DEBUG NHibernate.Engine.QueryParameters - named parameters: {}
2012-08-22 16:22:28,210 [15] DEBUG rollingFile - End of retrieval
4

1 に答える 1

1

マッピングに問題があるようです。
あなたの状況を再現しようとしましたが、hbm ファイルを少し変更しました

出版社

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="CartManData"
                   namespace="CartManData.Domain">

    <class name="Publisher" lazy="false">
        <id name="Id">
            <generator class="guid"></generator>
        </id>
        <property name="Name"></property>
        <property name="AddressLine1"></property>
        <property name="AddressLine2"></property>
        <property name="AddressLine3"></property>
        <property name="Town"></property>
        <property name="PostCode"></property>
        <property name="Telephone"></property>
        <property name="Email"></property>
        <property name="Enabled"></property>
        <property name="CommissionRate"></property>

        <set name="Coupons" cascade="all-delete-orphan" inverse="true" lazy="false">
            <key column="PublisherId" />
            <one-to-many class="CartManData.Domain.Coupon, CartManData" />
        </set>

    </class>
</hibernate-mapping>

クーポン

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="CartManData"
                   namespace="CartManData.Domain">

    <class name="Coupon">
        <id name="Id">
            <generator class="guid"></generator>
        </id>
        <property name="Name"></property>
        <property name="EffectiveFrom"></property>
        <property name="EffectiveTo"></property>
        <property name="UnitPrice"></property>
        <property name="OriginalPrice"></property>
        <property name="CouponImage"></property>
        <property name="Enabled"></property>

        <many-to-one class="CartManData.Domain.Publisher, CartManData" name="Publisher">
            <column name="PublisherId" not-null="true" />
        </many-to-one>

    </class>
</hibernate-mapping>

ご覧のとおり、クーポンで多対 1 の関係を使用しました。
これらの関係がどのように機能するかについては、こちら を参照してください。Publisher マッピング用に定義されたセットで inverse="true" を使用しました。inverseに関するその他の情報。

2つのクラスを見たい場合:

public class Publisher
{
    public Publisher()
    {
        this.Coupons = new HashSet<Coupon>();
    }

    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string AddressLine1 { get; set; }
    public virtual string AddressLine2 { get; set; }
    public virtual string AddressLine3 { get; set; }
    public virtual string Town { get; set; }
    public virtual string PostCode { get; set; }
    public virtual string Telephone { get; set; }
    public virtual string Email { get; set; }
    public virtual bool Enabled { get; set; }
    public virtual decimal CommissionRate { get; set; }

    public virtual ICollection<Coupon> Coupons { get; set; }
}

public class Coupon
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime EffectiveFrom { get; set; }
    public virtual DateTime EffectiveTo { get; set; }
    public virtual double UnitPrice { get; set; }
    public virtual double OriginalPrice { get; set; }
    public virtual string CouponImage { get; set; }
    public virtual bool Enabled { get; set; }

    public virtual Publisher Publisher { get; set; }

}

ここから実際のサンプル ( NHVariousTests )をダウンロードできます。

于 2012-08-28T14:10:47.873 に答える