简体   繁体   English

C#-NHibernate带来多对多关系的多个记录

[英]C# - NHibernate brings multiple records on many-to-many relationship

Basically, I'm trying to pull the records from a table with a many-to-many relationship with itself. 基本上,我试图从与自身具有多对多关系的表中提取记录。 It's a product table, which has to be linked to many ingredients (other products). 这是一个产品表,必须将其链接到许多成分(其他产品)。 The problem is that when I pull the data from a product that has more than one ingredient linked to it, NHibernate returns me one instance of the object for each ingredient the product has. 问题是,当我从链接了多个成分的产品中提取数据时,NHibernate会针对该产品具有的每种成分向我返回一个对象实例。 Here's how I have my class mapped and structured: 这是我的班级映射和结构的方式:

<?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; }
    }
}

And this is how I'm pulling the data from the database: 这就是我从数据库中提取数据的方式:

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

Does anyone have the slightest idea of why? 有人对为什么有丝毫想法吗? What am I doing wrong? 我究竟做错了什么?

In your query, specify that the DistinctRootEntityTransformer should be used: 在查询中,指定应使用DistinctRootEntityTransformer:

return Session
        .CreateCriteria<Product>()
        .SetFetchMode("ingredients", FetchMode.Eager)
        .SetFetchMode("suppliers", FetchMode.Eager)
        .SetResultTransformer(Transformers.DistinctRootEntity)
        .List<Product>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM