With the release of Fluent nHibernate 1.0 RC (download here) there are some great changes to the mapping structure of Subclass and JoinedSubclass. This excerpt from the 1.0 RC release notes gives a highlight of what those changes are and what is expected of us:
Separated subclass mapping – Subclasses can (and should be) defined separately from their parent mapping. Use SubclassMap<T> the same way as you would ClassMap<T>; if the top-most mapping (ClassMap) contains a DiscriminateSubclassesOnColumn call, the subclasses will be mapped as table-per-class-hierarchy, otherwise (by default) they’ll be mapped as table-per-subclass.
Okay so now we have quite a bit of info, but what does it all mean for the code. For those of you who 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. Which are pretty simple and vary only slightly between the two approaches. This allows you to change mapping styles very quickly and flexibly.
The Mappings:
The Mappings for the Subclasses pretty much stay the same. The optional column name portions of the mapping are most helpful in mapping Table Per Class Hierarchy.
public class BankAccountMap : SubclassMap<BankAccount> { public BankAccountMap() { Map(x => x.BankName, "BankAccount_BankName"); Map(x => x.RoutingNumber, "BankAccount_RoutingNumber"); } }
public class CreditCardMap : SubclassMap<CreditCard> { public CreditCardMap() { Map(x => x.Type, "CreditCard_Type"); Map(x => x.ExpirationMonth, "CreditCard_ExpirationMonth"); Map(x => x.ExpirationYear, "CreditCard_ExpirationYear"); } }
Here you can see the base class mapping for BillingDetails. To use a JoinedSubclass mapping as opposed to the Subclass mapping you just simply omit the DiscriminateSubClassesOnColumn() call.
public class BillingDetailsMap : ClassMap<BillingDetails> { public BillingDetailsMap() { Id(x => x.Id); Map(x => x.Number); Map(x => x.Owner); Map(x => x.DateCreated); DiscriminateSubClassesOnColumn("BillingDetailsType"); } }










