I'm trying to get NHibernate (3.3.1) to load a recursive parent/child category relationship.
public class Category
{
public virtual int Id { get; set; }
public virtual bool IsActive { get; set; }
public virtual string Name { get; set; }
public virtual Category Parent { get; set; }
public virtual IList<Category> Children { get; set; }
public Category()
{
Children = new List<Category>();
}
public virtual int GetChildCount()
{
return Children.Count;
}
}
And my XML Mapping...
<class name="nHibernatePOC.Domain.Category, nHibernatePOC" lazy="true">
<id name="Id" column="CategoryId">
<generator class="identity" />
</id>
<property name="Name" column="Name" />
<property name="IsActive" column="IsActive" />
<many-to-one name="Parent" class="nHibernatePOC.Domain.Category" column="ParentCategoryId" />
<bag lazy="true" name="Children">
<key column="ParentCategoryId" />
<one-to-many class="nHibernatePOC.Domain.Category" />
<loader query-ref="GetCategoryByParentId"/>
</bag>
</class>
My issue is when I try to access Children.Count
I get a NullReferenceException
because the Parent
can be null.
For future reference, the problem was related to the stored procedure that was used to load the child collection. (Rather than lazy loading from a table / view). Thankfully I am in a position to load from a view instead of using a Stored Procedure.
The moment I removed the <loader
element and reverted to using a view from the database (MSSQL 2012) it worked as expected.
Very odd, sorry to anyone who needs to use stored procedures but I'm guessing it's a good reason to remove them in this case.