NHibernate集合提取,重复数据

Cha*_*ish 2 .net c# nhibernate

我有一个复杂的情况,我需要用NHibernate映射三个不同的类.Class1(Branch.cs)具有Class2(Employee.cs)对象的集合.同时Class2还有一个Class3(Contacts.cs)对象的集合.
由于数据非常庞大,我使用fetch关键字在单个查询中检索数据.
我使用查询作为

Query1 - "从分支b内部联接获取b.Employee e内部联接获取e.Contacts" - 单个查询但重复的结果.
Query2 - "来自分支b内部联接获取b.Employee" - 多个查询,需要的结果.

我在映射文件中使用了包.查询结果似乎有重复的结果.如何删除重复的结果以及在单个查询中检索数据.我包括映射文件和类.

Contacts.hbm.xml

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample">
  <class name="Contacts" table="Contacts">
      <id name="EmployeeID"/>
      <property name="EmployeeID"/>
      <property name="Mobile"/>
      <property name="Alternate"/>
  </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

Branch.hbm.xml

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample">
  <class name="Branch" table="Branch">
      <id name="BranchCode"/>
      <property name="BranchCode"/>
      <property name="BranchName"/>
      <bag name="EmployeeList" cascade="all-delete-orphan" inverse="false" batch-size="10000">
          <key column="BranchCode"/>
          <one-to-many class="Employee" />
      </bag>
  </class>

</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

Employee.hbm.xml

    <?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample">
  <class name="Employee" table="Employee">
      <id name="EmployeeId"/>
      <property name="EmployeeId"/>
      <property name="FirstName"/>
      <property name="LastName"/>
      <property name="BranchCode"/>
      <bag name="Contact" cascade="all-delete-orphan" inverse="false" batch-size="10000">
          <key column="EmployeeID"/>
          <one-to-many class="Contacts" />
      </bag>
  </class>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)

Contacts.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace NHibernateSample
{
    public class Contacts
    {
        String employeeID;
        String mobile;
        String alternate;

        public Contacts()
        { }


        public virtual String EmployeeID
        {
            get { return employeeID; }
            set { employeeID = value; }
        }

        public virtual String Mobile
        {
            get { return mobile; }
            set { mobile = value; }
        }        
        public virtual String Alternate
        {
            get { return alternate; }
            set { alternate = value; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Employee.cs

    using System;
using System.Collections.Generic;
using System.Text;

namespace NHibernateSample
{
    public class Employee
    {
        String employeeId;
        String firstName;
        String lastName;
        String branchCode;
        List<Contacts> contact = new List<Contacts>();

        public virtual List<Contacts> Contact
        {
            get { return contact; }
            set { contact = value; }
        }
        public virtual String EmployeeId
        {
            get { return employeeId; }
            set { employeeId = value; }
        }

        public virtual String FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public virtual String LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        public virtual String BranchCode
        {
            get { return branchCode; }
            set { branchCode = value; }
        }

        public Employee()
        { }
    }
}
Run Code Online (Sandbox Code Playgroud)

Branch.cs

    using System.Collections.Generic; 
using System.Text; 
using System; 


namespace NHibernateSample 
{
    [Serializable]
    public class Branch
    {
        private String branchCode;
        private String branchName;
        private IList<Employee> employeeList = new List<Employee>();

        public virtual IList<Employee> EmployeeList
        {
            get { return employeeList; }
            set { employeeList = value; }
        }
        public virtual String BranchCode
        {
            get { return branchCode; }
            set { branchCode = value; }
        }

        public virtual String BranchName
        {
            get { return branchName; }
            set { branchName = value; }
        }

        public Branch() { }
    }
}
Run Code Online (Sandbox Code Playgroud)

Man*_*ieh 5

这是NHibernate的问题,他们无法修复.

您可以通过应用DistinctEntityTransformer在第一级获得正确的结果,这是NHibernate中的内置转换器,您应该使用它将它应用于查询对象

QueryOver.TranformUsing(Tranformers.DistinctEntityTransformer).
Run Code Online (Sandbox Code Playgroud)

在诸如你的问题之类的多级别上,你需要编写自己的变换器并使用它来代替不同的实体变换器.您可以使用类似的逻辑到NHibernate提供的不同实体转换器来修复细节上的复制.

编辑:这是一个实现:

public class MultiLevelDistinctEntityTransformer : IResultTransformer
{
    private readonly Dictionary<Type, List<String>> _fetchedCollectionProperties; // used to know which properties are fetched so you don't fetch more details than required

    public MultiLevelDistinctEntityTransformer(Dictionary<Type, List<String>> fetchedCollectionProperties)
    {
        _fetchedCollectionProperties = fetchedCollectionProperties;
    }

    public object TransformTuple(object[] tuple, string[] aliases)
    {
        return tuple.Last();
    }

    public IList TransformList(IList list)
    {
        if (list.Count == 0)
            return list;
        var result = (IList) Activator.CreateInstance(list.GetType());
        var distinctSet = new HashSet<Entity>();
        foreach (object item in list)
        {
            var entity = item as Entity; // Entity is the base class of my nhibernate classes
            if (entity == null)
                continue;
            if (distinctSet.Add(entity))
            {
                result.Add(item);
                HandleItemDetails(item);
            }
        }
        return result;
    }

    private void HandleItemDetails(object item)
    {
        IEnumerable<PropertyInfo> collectionProperties =
            item.GetType().GetProperties().Where(
                prop =>
                prop.IsCollectionProperty()/*extension method which checks if the object inherits from ICollection*/ &&
                _fetchedCollectionProperties.ContainsKey(item.GetType()) &&// get only the fetched details

                _fetchedCollectionProperties[item.GetType()].Contains(prop.Name));
        foreach (PropertyInfo collectionProperty in collectionProperties)
        {
            dynamic detailList = collectionProperty.GetValue(item, null);
            if (detailList != null)
            {
                dynamic uniqueValues =
                    Activator.CreateInstance(
                        typeof (List<>).MakeGenericType(collectionProperty.PropertyType.GetGenericArguments()[0]));
                var distinct = new HashSet<Entity>();
                foreach (var subItem in detailList)
                {
                    var entity = subItem as Entity;
                    if (distinct.Add(entity))
                    {
                        uniqueValues.Add(subItem);
                        HandleItemDetails(subItem);
                    }
                }
                collectionProperty.SetValue(item, uniqueValues, null);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)