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

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

基本上,我试图从与其自身具有多对多关系的表中提取记录。这是一个产品表,必须链接到许多成分(其他产品)。问题是,当我从链接了多种成分的产品中提取数据时,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>();
}

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

最佳答案

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

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

关于C# - NHibernate带来多对多关系的多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12514650/

相关文章:

c# - 自托管 SignalR 有哪些选择?

c# - nuget - 找不到 Microsoft.Windows.UI.Xaml.CSharp.targets

c# - 如何在 C# 中的表单中使用具有不同控件的多个面板

mysql - 如何从 mysqldump 生成 xml 文件以及在哪里可以找到该文件?

php - 使用 php mysql 查看出勤日期

java - Spring Data Jpa 选择新问题

java - JPQL,有没有一种方法可以过滤子集合,而不同时过滤不满足条件的父集合?

c# - 如何从 GUID 生成 8 字节的唯一 ID?

PHP MYSQL 如果存在则更新或如果不存在则插入?

java - 数据库连接错误