Ang3lFir3 – Life as a Code Poet

August 6, 2009

Table Per Subclass Inheritance Mapping with Fluent nHibernate

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

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

This is the third 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 :


  • 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 Subclass example. In Table per Subclass inheritance mapping we represent inheritance by using a foreign key to join tables together as needed.  This most closely represents the object model in the relational model of the database by using a table for the base class and one for each subclass.


Here you can see that again I only have one mapping file which covers the entire class Hierarchy.

Below you can see the Fluent Mappings for this approach. The JoinedSubClass<T> uses the Type as the table name by default (“CreditCard” and “BankAccount”) and the first parameter of the method is the name to use for the foreign key column. Additionally the mapping for the class being joined is included.

  public class BillingDetailsMap : ClassMap<BillingDetails>
    public BillingDetailsMap()
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      JoinedSubClass<CreditCard>("CreditCard_Id", m =>
                                                      m.Map(c => c.Type);
                                                      m.Map(c => c.ExpirationMonth);
                                                      m.Map(c => c.ExpirationYear);
      JoinedSubClass<BankAccount>("BankAccount_Id", m =>
                                                        m.Map(b => b.BankName);
                                                        m.Map(b => b.RoutingNumber);

Which maps to the following nHibernate XML mapping file.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerSubclass.Model.BillingDetails, TablePerSubclass, Version=, Culture=neutral, PublicKeyToken=null" table="`BillingDetails`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="Id">
      <generator class="identity" />
    <property name="Number" type="String">
      <column name="Number" />
    <property name="Owner" type="String">
      <column name="Owner" />
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    <joined-subclass name="TablePerSubclass.Model.CreditCard, TablePerSubclass, Version=, Culture=neutral, PublicKeyToken=null">
      <key column="CreditCard_Id" />
      <property name="Type" type="Int32">
        <column name="Type" />
      <property name="ExpirationMonth" type="String">
        <column name="ExpirationMonth" />
      <property name="ExpirationYear" type="String">
        <column name="ExpirationYear" />
    <joined-subclass name="TablePerSubclass.Model.BankAccount, TablePerSubclass, Version=, Culture=neutral, PublicKeyToken=null">
      <key column="BankAccount_Id" />
      <property name="BankName" type="String">
        <column name="BankName" />
      <property name="RoutingNumber" type="String">
        <column name="RoutingNumber" />

And the corresponding Database Schema. I followed the naming from the NHibernate in Action so you can see here maybe this is a little confusing. But basicly the “BankAccount_Id” is the primary key of the BankAccount table and contains is also a foreign key to the BillingDetails table.


kick it on

About these ads


  1. Sorry but that is not a real NH xml mapping. It is only the mapping generated by FNH.

    Comment by Fabio Maulo — August 7, 2009 @ 8:17 pm

    • what makes the hbm.xml files generated by FNH not actual nHibernate mapping files? I ask so that I can understand your comment.

      Comment by ang3lfir3 — August 8, 2009 @ 12:47 am

      • Fluent NHibernate’s generated HBM is more verbose than you’d normally create if writing it yourself. Properties don’t need the explicit column element. The type attribute is generally unneeded, and the joined-subclass name can just be the type name.

        Comment by James Gregory — August 8, 2009 @ 12:59 am

      • Most of which is to be expected with xml being generated from code … but I would think they are still actual nHibernate mapping files. Just very verbose ones.

        Comment by ang3lfir3 — August 8, 2009 @ 1:06 am

      • They are still mapping files, quite obviously. You’ll need Fabio to explain that one.

        Comment by James Gregory — August 8, 2009 @ 1:14 am

  2. [...] 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. Which are pretty simple and vary only slightly between the two [...]

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

  3. What if the subclass table name is different than the subclass object Name ?
    How can I Tell FNH that CreditCard data is in tblCreditCard ?

    my problem implementing this example is that it can’t find my table as it looks for a table name named CreditCard….

    Comment by Dani — September 8, 2009 @ 9:22 am

  4. Fastidious respond in return of this question with genuine arguments and explaining all about that.|

    Comment by ddos vps — August 4, 2013 @ 9:16 pm

  5. I think this is among the most important information for
    me. And i am glad reading your article. But should remark on
    some general things, The site style is great, the articles is really nice
    : D. Good job, cheers

    Comment by bongs — April 16, 2014 @ 10:09 pm

  6. What’s Going down i’m new to this, I stumbled upon this I have discovered It positively useful
    and it has helped me out loads. I hope to contribute & aid other customers like its helped me.
    Great job.

    Comment by Jule S. Gobler — September 23, 2014 @ 4:24 pm

  7. Good day! This is my first comment here so I just wanted to give a quick shout out and say I genuinely
    enjoy reading through your posts. Can you recommend any other blogs/websites/forums that deal with
    the same topics? Thanks a ton!

    Comment by Mamie W. Molder — September 25, 2014 @ 2:18 pm

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

Theme: Silver is the New Black. Get a free blog at


Get every new post delivered to your Inbox.

%d bloggers like this: