Why is my IList <T> returned as a list of null objects?
As long as I understand that Set or Bag is probably the right way to do this, I'm new to NHibernate and I'm trying to figure out why the following happens.
I have two classes:
public class Customer
{
public virtual int Id { get; protected set; }
public virtual string CustomerName { get; set; }
// Customer has many domains
public virtual IList<Domain> Domains { get; set; }
}
public class Domain
{
public virtual int Id { get; protected set; }
public virtual int CustomerID { get; set; }
public virtual string DomainName { get; set; }
}
My mapping files are as follows:
<!-- Domain -> tblDomains -->
<class name="Domain" table="tblDomains">
<id name="Id">
<column name="DomainID" sql-type="int" not-null="true"/>
<generator class="identity"/>
</id>
<property name="CustomerID"/>
<property name="DomainName"/>
</class>
<!-- Customer -> tblCustomer -->
<class name="Customer" table="tblCustomer">
<id name="Id">
<column name="CustomerID" sql-type="int" not-null="true"/>
<generator class="identity"/>
</id>
<property name="CustomerName" column="Customer"/>
<list name="Domains">
<key column="CustomerID"/>
<index column="DomainID"/>
<one-to-many class="Domain" />
</list>
</class>
When I return the object Customer, the property Domainscontains a list of 665383 null objects Domain. Element 665384'th in the list contains a valid population.
Only 63 Domainbelong to this client, so I assume that this is some kind of Cartesian product result. I looked into SQL at NHProfiler, but all I see is a query that looks pretty innocent when I iterate over the first item in a list Domains:
SELECT domains0_.CustomerID as CustomerID1_,
domains0_.DomainID as DomainID1_,
domains0_.DomainID as DomainID2_0_,
domains0_.CustomerID as CustomerID2_0_,
domains0_.DomainName as DomainName2_0_
FROM tblDomains domains0_
WHERE domains0_.CustomerID = 5667 /* @p0 */
<bag>, . - , ?
+3
1