我一直在試圖用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。誰能幫忙?
我以前找過這個帖子。我最初在閱讀「下一個/前一個兄弟」問題時閱讀它。它仍然不會處理與當前對象相同級別的節點。我想知道是否我必須得到父母,然後是孩子。 – 2009-03-01 03:59:46