title: Inheritance Mapping with Fluent NHibernate
body: <p>Given the following scenario, I want map the type hierarchy to the database schema using Fluent NHibernate.</p>
  
  <p>I am using NHibernate 2.0</p>
  
  <hr>
  
  <p><strong>Type Hierarchy</strong></p>
  
  <pre><code>public abstract class Item
  {
      public virtual int ItemId { get; set; }
      public virtual string ItemType { get; set; }
      public virtual string FieldA { get; set; }
  }
  
  public abstract class SubItem : Item
  {
      public virtual string FieldB { get; set; } 
  }
  
  public class ConcreteItemX : SubItem
  {
      public virtual string FieldC { get; set; } 
  }
  
  public class ConcreteItemY : Item
  {
      public virtual string FieldD { get; set; }
  }
  </code></pre>
  
  <p><a href="http://www.freeimagehosting.net/uploads/3e9efc1fee.png" rel="noreferrer">See image</a></p>
  
  <p>The <code>Item</code> and <code>SubItem</code> classes are abstract.</p>
  
  <hr>
  
  <p><strong>Database Schema</strong></p>
  
  <pre>
  +----------+  +---------------+  +---------------+
  | Item     |  | ConcreteItemX |  | ConcreteItemY |
  +==========+  +===============+  +===============+
  | ItemId   |  | ItemId        |  | ItemId        |
  | ItemType |  | FieldC        |  | FieldD        |
  | FieldA   |  +---------------+  +---------------+
  | FieldB   |
  +----------+
  </pre>
  
  <p><a href="http://www.freeimagehosting.net/uploads/10644fe741.png" rel="noreferrer">See image</a></p>
  
  <p>The <code>ItemType</code> field determines the concrete type.</p>
  
  <p>Each record in the <code>ConcreteItemX</code> table has a single corresponding record in the <code>Item</code> table; likewise for the <code>ConcreteItemY</code> table.</p>
  
  <p><code>FieldB</code> is always null if the item type is <code>ConcreteItemY</code>.</p>
  
  <hr>
  
  <p><strong>The Mapping (so far)</strong></p>
  
  <pre><code>public class ItemMap : ClassMap&lt;Item&gt;
  {
      public ItemMap()
      {
          WithTable("Item");
          Id(x =&gt; x.ItemId, "ItemId");
          Map(x =&gt; x.FieldA, "FieldA");
  
          JoinedSubClass&lt;ConcreteItemX&gt;("ItemId", MapConcreteItemX);
          JoinedSubClass&lt;ConcreteItemY&gt;("ItemId", MapConcreteItemY);
      }
  
      private static void MapConcreteItemX(JoinedSubClassPart&lt;ConcreteItemX&gt; part)
      {
          part.WithTableName("ConcreteItemX");
          part.Map(x =&gt; x.FieldC, "FieldC");
      }
  
      private static void MapConcreteItemY(JoinedSubClassPart&lt;ConcreteItemY&gt; part)
      {
          part.WithTableName("ConcreteItemX");
          part.Map(x =&gt; x.FieldD, "FieldD");
      }
  }
  </code></pre>
  
  <p><code>FieldB</code> is not mapped.</p>
  
  <hr>
  
  <p><strong>The Question</strong></p>
  
  <p>How do I map the <code>FieldB</code> property of the <code>SubItem</code> class using Fluent NHibernate?</p>
  
  <p>Is there any way I can leverage <code>DiscriminateSubClassesOnColumn</code> using the <code>ItemType</code> field?</p>
  
  <hr>
  
  <p><strong>Addendum</strong></p>
  
  <p>I am able to achieve the desired result using an hbm.xml file:</p>
  
  <pre><code>&lt;class name="Item" table="Item"&gt;
  
    &lt;id name="ItemId" type="Int32" column="ItemId"&gt;
      &lt;generator class="native"/&gt;
    &lt;/id&gt;
  
    &lt;discriminator column="ItemType" type="string"/&gt;
  
    &lt;property name="FieldA" column="FieldA"/&gt;
  
    &lt;subclass name="ConcreteItemX" discriminator-value="ConcreteItemX"&gt;
      &lt;!-- Note the FieldB mapping here --&gt;
      &lt;property name="FieldB" column="FieldB"/&gt;
      &lt;join table="ConcreteItemX"&gt;
        &lt;key column="ItemId"/&gt;
        &lt;property name="FieldC" column="FieldC"/&gt;
      &lt;/join&gt;
    &lt;/subclass&gt;
  
    &lt;subclass name="ConcreteItemY" discriminator-value="ConcreteItemY"&gt;
      &lt;join table="ConcreteItemY"&gt;
        &lt;key column="ItemId"/&gt;
        &lt;property name="FieldD" column="FieldD"/&gt;
      &lt;/join&gt;
    &lt;/subclass&gt;
  
  &lt;/class&gt;
  </code></pre>
  
  <p>How do I accomplish the above mapping using Fluent NHibernate?</p>
  
  <p>Is it possible to mix table-per-class-hierarchy with table-per-subclass using Fluent NHibernate?</p>