0

基本的に、テーブル自体と多対多の関係を持つテーブルからレコードを取得しようとしています。これは、多くの成分 (他の製品) にリンクする必要がある製品テーブルです。問題は、複数の成分がリンクされている製品からデータを取得すると、NHibernate が、製品に含まれる成分ごとにオブジェクトのインスタンスを 1 つ返すことです。クラスをマップして構造化する方法は次のとおりです。

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

  <class name="Product" table="product">
    <id name="id" generator="identity">
      <column name="id" not-null="true" />
    </id>

    <property name="name">
      <column name="name" length="128" not-null="true" />
    </property>

    <property name="stock">
      <column name="stock" not-null="true" />
    </property>

    <property name="value">
      <column name="value" not-null="true" />
    </property>

    <many-to-one name="category" class="Category" column="category" not-null="true" fetch="join" lazy="false" />



    <!-- Relations -->
    <set name="ingredients" table="product_x_ingredient" fetch="join" lazy="true">
      <key column="product_id" />
      <many-to-many class="Product" column="ingredient_id" />
    </set>

    <set name="suppliers" table="product_x_supplier" fetch="join" lazy="true">
      <key column="product_id" />
      <many-to-many class="Supplier" column="supplier_id" />
    </set>

    <set name="saleItems" lazy="true" inverse="true">
      <key column="product_id" />
      <one-to-many class="SaleItem" />
    </set>

    <set name="stockInlets" lazy="true" inverse="true">
      <key column="product" />
      <one-to-many class="StockInlet" />
    </set>
  </class>

namespace TCC.Hibernate.Domain.Classes
{
    class Product
    {
        public Product()
        {
            suppliers = new HashSet<Supplier>();
            ingredients = new HashSet<Product>();
        }

        public virtual uint id { get; set; }
        public virtual string name { get; set; }
        public virtual float stock { get; set; }
        public virtual float value { get; set; }
        public virtual Category category { get; set; }

        public virtual ICollection<Supplier> suppliers { get; set; }
        public virtual ICollection<Product> ingredients { get; set; }
        public virtual ISet saleItems { get; set; }
        public virtual ISet stockInlets { get; set; }
    }
}

そして、これは私がデータベースからデータを引き出す方法です:

using (ISession Session = NHibernateHelper.OpenSession())
{
    return Session
        .CreateCriteria<Product>()
        .SetFetchMode("ingredients", FetchMode.Eager)
        .SetFetchMode("suppliers", FetchMode.Eager)
        .List<Product>();
}

誰かが理由を少しでも知っていますか? 私は何を間違っていますか?

4

1 に答える 1

1

クエリで、DistinctRootEntityTransformerを使用するように指定します。

return Session
        .CreateCriteria<Product>()
        .SetFetchMode("ingredients", FetchMode.Eager)
        .SetFetchMode("suppliers", FetchMode.Eager)
        .SetResultTransformer(Transformers.DistinctRootEntity)
        .List<Product>();
于 2012-09-20T14:26:46.930 に答える