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.
<loader query-ref=be the problem. Do you really need that or can you use a formula on the key? - FiroNullReferenceExceptionis in regards to the Parent property, the children is initialised with an empty collection. Also the loader is required since the data comes from stored procedures not direct table mappings. - Sam Lad