HierarchyIdの直接の子孫にデータを入力するための最良の方法を決定したと思います。この機能を機能させるには、クラスにフィルターを作成する必要があり、クラスのメソッドを実行して子孫を照会します。
NHibernateでのHierarchyIdサポートの設定に関する詳細情報。
子のマッピング
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CentralDataLayer" namespace="NHibernate.Map" schema="`global`">
<class name="NHibernate.Map.OrganizationUnit, CentralDataLayer" table="`orgunit`">
...
<property name="HierarchyId" column="`ou_hid`" type="NHibernate.UserTypes.HierarchyId, CentralDataLayer" not-null="true" />
<filter name="FilterChildren" condition="`ou_hid`.IsDescendantOf(:HierarchyId) = 1 and `ou_hid`.GetLevel() = :HierarchyIdLevel" />
</class>
<filter-def name="FilterChildren">
<filter-param name="HierarchyId" type="NHibernate.UserTypes.HierarchyId, CentralDataLayer" />
<filter-param name="HierarchyIdLevel" type="Int16" />
</filter-def>
</hibernate-mapping>
クラスオブジェクト
namespace NHibernate.Map
{
using Criterion;
using Microsoft.SqlServer.Types;
using NHibernate;
using System.Collections.Generic;
public partial class OrganizationUnit
{
#region Fields
private SqlHierarchyId _hierarchyId;
private ICollection<OrganizationUnit> _children;
#endregion Fields
#region Properties
/// <summary>
/// Hierarchy Id
/// </summary>
[Description("Get the OrganizationUnit hierarchy id.")]
public virtual SqlHierarchyId HierarchyId
{
get { return _hierarchyId; }
protected internal set { _hierarchyId = value; }
}
#endregion Properties
#region Public Methods
/// <summary>
/// Get all direct child OrganizationUnits of current OrganizationUnit.
/// </summary>
/// <param name="provider">The DataProvider houseing the current Session</param>
/// <returns></returns>
public virtual ICollection<OrganizationUnit> GetChildren(ISession session)
{
if (_children.IsDefault()) {
// Enable Filter
session
.EnableFilter("FilterChildren")
.SetParameter("HierarchyId", HierarchyId)
.SetParameter("HierarchyIdLevel", (short)(HierarchyId.GetLevel() + 1));
// Collect Direct Children
_children = session.QueryOver<OrganizationUnit>().List();
// Disable Filter
session.DisableFilter("FilterChildren");
}
return _children;
}
#endregion Public Methods
}
}