我有一个复杂的情况,我需要用 NHibernate 映射三个不同的类。Class1(Branch.cs) 具有 Class2(Employee.cs) 对象的集合。同时 Class2 也有 Class3(Contacts.cs) 对象的集合。
由于数据非常庞大,我使用 fetch 关键字在单个查询中检索数据。
我将查询用作
Query1 -“从 Branch b 内部联接获取 b.Employee e 内部联接获取 e.Contacts” -单个查询但结果重复。
Query2 - “从分支 b 内部连接获取 b.Employee” - 多个查询,需要的结果。
我在映射文件中使用了包。查询结果似乎有重复的结果。如何删除重复结果以及在单个查询中检索数据。我包括映射文件以及类。
联系人.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>
分支.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>
员工.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>
联系人.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; }
}
}
}
员工.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()
{ }
}
}
分支.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() { }
}
}