title: NHibernate mapping - self referencing: parent and children
body: <p>I am trying to have this kind of Model:</p>
  
  <pre><code>public class Activity
  {
      public virtual int ID { get; set; }
  
      public virtual int? ParentID { get; set; }
      public virtual int? RootID { get; set; }
  
      public virtual Activity Parent { get; set; }
      public virtual Activity Root { get; set; }
      public virtual IList&lt;Activity&gt; Children { get; set; }
  }
  </code></pre>
  
  <p>If you are looking at it from a structure point of view, it is a tree.
  The root element does not have a parent or a root, but may have children.
  Any of its children must have a parent and a root (for the first level children root = parent)</p>
  
  <p>The mapper is like this:</p>
  
  <pre><code>public class ActivityMap : ClassMapping&lt;Activity&gt;
  {
      public ActivityMap()
      {
          Table("activity");
          Lazy(true);
          Id(x =&gt; x.ID, map =&gt; map.Generator(Generators.Identity));
  
          ManyToOne(x =&gt; x.Root, map =&gt; { map.Column("RootID"); map.Cascade(Cascade.All); });
  
          Bag(x =&gt; x.Children,
             mapping =&gt;
             {
                 mapping.Inverse(false);
                 mapping.Lazy(CollectionLazy.Lazy);
                 mapping.Key(k =&gt; k.Column("ParentID"));
                 mapping.Cascade(Cascade.All);
             },
             mapping =&gt; mapping.ManyToMany(map=&gt;map.Class(typeof(Activity)))
             );
      }
  }
  </code></pre>
  
  <p>The problem is when I try to fetch the children, the sql statement looks like:</p>
  
  <pre><code>SELECT children0_.ParentID as ParentID1_,
  children0_.elt as elt1_, 
  activity1_.ID as ID55_0_, 
  activity1_.TaskID as TaskID55_0_, 
  activity1_.ActivityTypeID as Activity3_55_0_, 
  activity1_.StateID as StateID55_0_, 
  activity1_.Continueforward as Continue5_55_0_, 
  activity1_.Ordernumber as Ordernum6_55_0_, 
  activity1_.IsDeleted as IsDeleted55_0_, 
  activity1_.Created as Created55_0_, 
  activity1_.Modified as Modified55_0_, 
  activity1_.StartTime as StartTime55_0_, 
  activity1_.EndTime as EndTime55_0_, 
  activity1_.Progress as Progress55_0_, 
  activity1_.RootID as RootID55_0_ 
  FROM Children children0_ left outer join activity activity1_ on children0_.elt=activity1_.ID WHERE children0_.ParentID=?
  </code></pre>
  
  <ol>
  <li>First of all it seems that it is looking for the Children table which does not exist. Should be Activity table</li>
  <li>Second: I am not sure what is with that "elt" column... it does not exist anywhere</li>
  </ol>
  
  <p>Anyone has an idea how to make this mapping?</p>
  
  <p><strong>Later Edit:</strong>
  found in answer to the second question:
  <a href="https://stackoverflow.com/questions/477146/nhibernate-elt-field">NHibernate elt field</a></p>