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







