我正在尝试让 NHibernate (3.3.1) 加载递归父/子类别关系。
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;
}
}
还有我的 XML 映射...
<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>
我的问题是当我尝试访问时,Children.Count
我得到了一个NullReferenceException
,因为它Parent
可以为空。