views:

223

answers:

1

I've been trying to build a SiteMapNode styled object with NHibernate mappings. The goal is to mimic the ASP.NET SiteMapNode object so that a custom provider can be built using NHibernate for a dynamic backend.

The issue I'm running into is the way the tree nature of a site map. The ASP.NET object has a next and previous sibling object. This is fine and well. I didn't want to have a NextSiblingId and PreviousSiblingId in my SiteMapNode table. I decided that when I display these objects it would be good to have an OrdinalPosition property. After research it seems like I cannot make a NextSibling and PreviousSibling property mapping in NHibernate. My thought to fix this is to make a Siblings collection. This collection would have the same ParentNodeId as the owner object.

Is this possible?

Here is the mapping file that I've come up with so far:

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

The Siblings bag returns the same thing as the ChildNodes collection. I'm just not understanding how the whole key and foreign key properties work. I figured that the column property on the key element was telling nHibernate to use that column on the owner object to map to the foreign object's column. I just have to find out how to make nHibernate look at the ParentNodeId on the collection's nodes. Can anyone help?

A: 

Check out this blog post: How to map a tree in NHibernate

Martinho Fernandes
I've found this post before. I originally read it when I was looking for the Next/Previous sibling issue. It still doesn't address nodes at the same level as the current object. I'm wondering if it is something where I have to get the parent and then it's children.
Mike G
That would work.
Martinho Fernandes