Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate Collection of collections fetching, Duplicate data
    text
    copied!<p>I have a complex situation, where I need to map three different classes with NHibernate. Class1(Branch.cs) has a collection of Class2(Employee.cs) objects. At the same time Class2 also has a collection of Class3(Contacts.cs) objects. <br/> As data is very huge I used fetch keyword to retrieve data in single query. <br/> I used the query as<br/> <br/><b>Query1 - "from Branch b inner join fetch b.Employee e inner join fetch e.Contacts"</b> - Single query but duplicate results. <br/><b>Query2 - "from Branch b inner join fetch b.Employee"</b> - Multiple query, required results. <br/> <br/> I have used bags in mapping files. The query result seems to having duplicate results. How to remove duplicate results as well as retrieving data in single query. I am including mapping files as well as Classes.</p> <p><strong>Contacts.hbm.xml</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample"&gt; &lt;class name="Contacts" table="Contacts"&gt; &lt;id name="EmployeeID"/&gt; &lt;property name="EmployeeID"/&gt; &lt;property name="Mobile"/&gt; &lt;property name="Alternate"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p><strong>Branch.hbm.xml</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample"&gt; &lt;class name="Branch" table="Branch"&gt; &lt;id name="BranchCode"/&gt; &lt;property name="BranchCode"/&gt; &lt;property name="BranchName"/&gt; &lt;bag name="EmployeeList" cascade="all-delete-orphan" inverse="false" batch-size="10000"&gt; &lt;key column="BranchCode"/&gt; &lt;one-to-many class="Employee" /&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p><strong>Employee.hbm.xml</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateSample" namespace="NHibernateSample"&gt; &lt;class name="Employee" table="Employee"&gt; &lt;id name="EmployeeId"/&gt; &lt;property name="EmployeeId"/&gt; &lt;property name="FirstName"/&gt; &lt;property name="LastName"/&gt; &lt;property name="BranchCode"/&gt; &lt;bag name="Contact" cascade="all-delete-orphan" inverse="false" batch-size="10000"&gt; &lt;key column="EmployeeID"/&gt; &lt;one-to-many class="Contacts" /&gt; &lt;/bag&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p><strong>Contacts.cs</strong></p> <pre><code>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; } } } } </code></pre> <p><strong>Employee.cs</strong></p> <pre><code> using System; using System.Collections.Generic; using System.Text; namespace NHibernateSample { public class Employee { String employeeId; String firstName; String lastName; String branchCode; List&lt;Contacts&gt; contact = new List&lt;Contacts&gt;(); public virtual List&lt;Contacts&gt; 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() { } } } </code></pre> <p><strong>Branch.cs</strong></p> <pre><code> using System.Collections.Generic; using System.Text; using System; namespace NHibernateSample { [Serializable] public class Branch { private String branchCode; private String branchName; private IList&lt;Employee&gt; employeeList = new List&lt;Employee&gt;(); public virtual IList&lt;Employee&gt; 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() { } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload