Ang3lFir3 – Life as a Code Poet

August 4, 2009

Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate

Filed under: .NET, C#, Fluent NHibernate, NHibernate, ORM, Patterns — Tags: , , , , , — ang3lfir3 @ 2:32 pm

Note: Updates for Fluent nHibernate 1.0 RC can be read here

This is the second in a short series of posts I am working on. While reading through NHibernate in Action I thought to my self that I should create these same examples of Inheritance mapping using Fluent nHibernate to show how simple these mappings can be.

The three types of Inheritance mappings that are mentioned on page 92 of NHibernate in Action are :Model

  • Table Per Concrete Class
  • Table Per Class Hierarchy
  • Table Per Subclass

I will be using the example from NHibernate in Action to illustrate the mappings. This post covers the Table Per Class Hierarchy example. In Table per Class Hierarchy  inheritance mapping we enable polymorphism by using a type discriminator column to determine the Type when rehydrating objects. This means that all of the properties of all the subclasses including those of the base class are included in the single table. All columns that are not part of the base class must be nullable allowing the single row to reflect only the data required for the type being stored.


Here you can see I have only one mapping file. This mapping file covers the entire class Hierarchy in a single file.

Below you can see the Fluent Mappings for this approach. The “DiscriminateSubClassOnColumn” indicates that name of the of the column in the Database that should be used to descriminate the types of each subclass. Fluent nHibernates default behaviour is to use the Class name as the descriminiator value. You can see how each Subclass is mapped. Notice that I used a prefix on the column names for the Subclass mappings. This allows the database schema to be easily readable.

  public class BillingDetailsMap : ClassMap<BillingDetails>
    public BillingDetailsMap()
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
        .SubClass<CreditCard>(m =>
                                  m.Map(c => c.Type, "CreditCard_Type");
                                  m.Map(c => c.ExpirationMonth, "CreditCard_ExpirationMonth");
                                  m.Map(c => c.ExpirationYear, "CreditCard_ExpirationYear");
        .SubClass<BankAccount>(m =>
                                   m.Map(b => b.BankName, "BankAccount_BankNumber");
                                   m.Map(b => b.RoutingNumber, "BankAccount_RoutingNumber");

Which maps to the following nHibernate XML mapping file.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerHierarchy.Model.BillingDetails, TablePerHierarchy, Version=, Culture=neutral, PublicKeyToken=null" table="`BillingDetails`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="Id">
      <generator class="identity" />
    <discriminator column="BillingDetailsType" type="String" />
    <property name="Number" type="String">
      <column name="Number" />
    <property name="Owner" type="String">
      <column name="Owner" />
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    <subclass name="TablePerHierarchy.Model.CreditCard, TablePerHierarchy, Version=, Culture=neutral, PublicKeyToken=null">
      <property name="Type" type="Int32">
        <column name="CreditCard_Type" />
      <property name="ExpirationMonth" type="String">
        <column name="CreditCard_ExpirationMonth" />
      <property name="ExpirationYear" type="String">
        <column name="CreditCard_ExpirationYear" />
    <subclass name="TablePerHierarchy.Model.BankAccount, TablePerHierarchy, Version=, Culture=neutral, PublicKeyToken=null">
      <property name="BankName" type="String">
        <column name="BankAccount_BankNumber" />
      <property name="RoutingNumber" type="String">
        <column name="BankAccount_RoutingNumber" />

And finally you can see the relational model this maps to. Every property of every type is represented as a column. Those of the base class are included as well. You can see the discriminator column “BillingDetailsType” as well.


kick it on



  1. […] just want to see the new mappings… I’ve got that for you. Using the same samples that I used in Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate and Table Per Subclass Inheritance Mapping with Fluent nHibernate I will show you the changes. […]

    Pingback by Updates to Subclass and JoinedSubclass mapping in Fluent nHibernate 1.0 RC « Ang3lFir3 – Life as a Code Poet — August 17, 2009 @ 3:21 pm

  2. Hi,

    The real kicker is its missing something:

    No discriminator … 😦

    I wonder if anyone has found a way of telling fnh what the discriminator value is for the sub classes

    Comment by Rei Roldan — September 5, 2009 @ 2:17 am

    • By default FNH uses the class names as the descriminator in the string (varchar) column. However there is an overloaded method that allows you to specify the type of the column. All of that was covered in this post.

      the descriminator is set as shown above:
      you could also do:
      .SubClass(1, m =>
      m.Map(c => c.Type, "CreditCard_Type");
      m.Map(c => c.ExpirationMonth, "CreditCard_ExpirationMonth");
      m.Map(c => c.ExpirationYear, "CreditCard_ExpirationYear");
      (i can’t verify the syntax since I just typed it from memory)

      If you read my post on the updates you will see that there have been changes to make these mappings easier.

      Comment by ang3lfir3 — September 6, 2009 @ 1:08 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: