【问题标题】:C# - NHibernate brings multiple records on many-to-many relationshipC# - NHibernate 带来多对多关系的多条记录
【发布时间】:2012-09-20 14:19:51
【问题描述】:

基本上,我试图从与自身具有多对多关系的表中提取记录。这是一个产品表,它必须与许多成分(其他产品)相关联。问题是,当我从具有多个关联的成分的产品中提取数据时,NHibernate 会为产品所具有的每种成分返回一个对象实例。以下是我的班级映射和结构:

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

有人知道为什么吗?我做错了什么?

【问题讨论】:

    标签: c# mysql hibernate nhibernate many-to-many


    【解决方案1】:

    在您的查询中,指定应使用 DistinctRootEntityTransformer:

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

    【讨论】:

    • 有没有办法将它设置为我创建的所有标准的默认值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2021-08-21
    • 2012-06-05
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多