nHibernate storage of an object with self referencing many children and many parents

Posted by AdamC on Stack Overflow See other posts from Stack Overflow or by AdamC
Published on 2010-06-09T05:06:25Z Indexed on 2010/06/09 5:12 UTC
Read the original article Hit count: 215

I have an object called MyItem that references children in the same item. How do I set up an nhibernate mapping file to store this item.

public class MyItem
    {
    public virtual string Id {get;set;}

    public virtual string Name {get;set;}

    public virtual string Version {get;set;}

    public virtual IList<MyItem> Children {get;set;}    

    }

So roughly the hbm.xml would be:

   <class name="MyItem"  table="tb_myitem">

      <id name="Id" column="id" type="String" length="32">
         <generator class="uuid.hex" />
      </id>

      <property name="Name"     column="name" />
      <property name="Version"  column="version" />

      <bag name="Children" cascade="all-delete-orphan" lazy="false">
         <key column="children_id" />
         <one-to-many class="MyItem"
                      not-found="ignore"/>
      </bag>      

   </class>

This wouldn't work I don't think. Perhaps I need to create another class, say MyItemChildren and use that as the Children member and then do the mapping in that class?

This would mean having two tables. One table holds the MyItem and the other table holds references from my item. NOTE: A child item could have many parents.

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate-mapping