如何在nHibernate中从HierarchyId填充子集的集合

时间:2012-11-28 16:38:42

标签: nhibernate nhibernate-mapping hierarchyid

我有映射的父方,但我不确定如何正确映射孩子方。

父映射

<many-to-one name="Parent" class="NHibernate.Map.OrganizationUnit, CentralDataLayer">
    <formula>(
        SELECT o.`ou_id`
        FROM `global`.`wmtb_orgunit` o
        WHERE o.`ou_hid` = `ou_hid`.GetAncestor(1)
    )</formula>
</many-to-one>

我认为孩子们会像这样映射。如何将父HierarchyId填充到where子句中?

儿童制图

<set name="Children" inverse="true" cascade="all" where="`ou_hid` != :HierarchyId and `ou_hid`.IsDescendantOf(:HierarchyId) = 1">
    <key column="`ou_hid`" />
    <one-to-many class="NHibernate.Map.OrganizationUnit, CentralDataLayer" />
</set>

1 个答案:

答案 0 :(得分:1)

我相信我已经确定了填充HierarchyId的直接后代的最佳方法。为了使这个功能起作用,需要在类上创建一个过滤器,并运行该类的方法来查询后代。

有关设置HierarchyId support in NHibernate的更多信息。

儿童制图

<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
    }
}