2009-03-01 58 views
0

我一直在试图用NHibernate映射构建一个SiteMapNode样式对象。目标是模仿ASP.NET SiteMapNode对象,以便可以使用NHibernate为动态后端构建自定义提供程序。在NHibernate中映射兄弟姐妹

我遇到的问题是网站地图的树性质。 ASP.NET对象具有下一个和前一个兄弟对象。这很好,很好。我不想在我的SiteMapNode表中有一个NextSiblingId和PreviousSiblingId。我决定,当我显示这些对象时,最好有一个OrdinalPosition属性。经过研究,似乎我无法在NHibernate中创建NextSibling和PreviousSibling属性映射。我想解决这个问题是制作一个兄弟姐妹系列。此集合将拥有与所有者对象相同的ParentNodeId。

这可能吗?

这里是我想出来的,到目前为止映射文件:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="AthletesCafe.Core.Domain.System.SiteMap" assembly="AthletesCafe.Core"> 

    <class name="SiteMapNode" table="SiteMapNode" lazy="true" > 

    <id name="ID" type="Int32" unsaved-value="0"> 
     <column name="ID" not-null="true" unique="true" index="PK_SiteMapNode"/> 
     <generator class="identity" /> 
    </id> 

    <property name="Title" column="Title" type="String" length="255" not-null="true" /> 
    <property name="Description" column="Description" type="String" not-null="false" /> 

    <property name="Url" column="Description" type="String" not-null="true" length="1000" /> 

    <property name="Key" column="NodeKey" type="String" not-null="true" length="255" /> 

    <property name="OrdinalPosition" column="OrdinalPosition" type="Int32" not-null="true" /> 

    <property name="ReadOnly" column="ReadOnly" not-null="true" type="System.Boolean" /> 

    <property name="IsExternal" column="IsExternal" not-null="true" type="System.Boolean" /> 

    <many-to-one name="ParentNode" column="ParentNodeId" class="AthletesCafe.Core.Domain.System.SiteMap.SiteMapNode, AthletesCafe.Core" 
       access="field.pascalcase-underscore" not-null="false" /> 

    <bag name="Siblings" access="field.pascalcase-underscore" inverse="true" lazy="true"> 
     <key column="ParentNodeId" /> 
     <many-to-many foreign-key="ParentNodeId" class="AthletesCafe.Core.Domain.System.SiteMap.SiteMapNode, AthletesCafe.Core" /> 
    </bag> 

    <bag name="ChildNodes" generic="true" inverse="true" lazy="true" access="field.pascalcase-underscore"> 
     <key column="ParentNodeId" /> 
     <one-to-many class="AthletesCafe.Core.Domain.System.SiteMap.SiteMapNode, AthletesCafe.Core"/> 
    </bag> 

    </class> 
</hibernate-mapping> 

兄弟姐妹包包返回同样的事情childNodes集合。我只是不理解整个密钥和外键属性如何工作。我发现key元素的column属性告诉nHibernate使用owner对象上的该列映射到外部对象的列。我只需要找出如何让nHibernate查看集合节点上的ParentNodeId。谁能帮忙?

回答

0

看看这个博客文章:How to map a tree in NHibernate

+0

我以前找过这个帖子。我最初在阅读“下一个/前一个兄弟”问题时阅读它。它仍然不会处理与当前对象相同级别的节点。我想知道是否我必须得到父母,然后是孩子。 – 2009-03-01 03:59:46