title: Mapping tree structure with Fluent NHibernate
body: <p>I am having trouble to map a tree structure using Fluent NHibernate, without also having to put in what I believe is a hack.</p>
  
  <p>Consider the following classes:</p>
  
  <ul>
  <li><code>Node</code> (abstract, has <code>int Id</code> and <code>GroupNode Parent</code> (if <code>null</code>, the node resides in the root node))</li>
  <li><code>GroupNode</code> (inherits from <code>Node</code>, has <code>IList&lt;GroupNode&gt; Groups</code> and <code>IList&lt;ItemNode&gt; Items</code>)</li>
  <li><code>ItemNode</code> (inherits from <code>Item</code>)</li>
  </ul>
  
  <p>Ideally, this would have the following database structure:</p>
  
  <ul>
  <li>GroupNodes (integer Id, nullable integer ParentId)</li>
  <li>ItemNodes (integer Id, nullable integer ParentId)</li>
  </ul>
  
  <p>My mappers looks like this:</p>
  
  <pre><code>public class GroupNodeMap : ClassMap&lt;GroupNode&gt;
  {
      public GroupNode()
      {
          Id(x =&gt; x.Id);
          References(x =&gt; x.Parent);
          HasMany(x =&gt; x.Groups).LazyLoad();
          HasMany(x =&gt; x.Items).LazyLoad();
      }
  }
  
  public class ItemNodeMap : ClassMap&lt;ItemNode&gt;
  {
      public ItemNodeMap()
      {
          Id(x =&gt; x.Id);
          References(x =&gt; x.Parent);
      }
  }
  </code></pre>
  
  <p>Unfortunately, this is creating a duplicate set of references (each table gets both a <code>ParentId</code> and a <code>GroupNodeId</code>. I can tweak this behaviour by adding <code>.KeyColumn("ParentId")</code> after <code>.LazyLoad()</code>, but this feels like a hack, and I would like it to be expressed using either conventions or lambdas instead of a magic string.</p>
  
  <p>Could anyone point me in the right direction?</p>