Subclass Difficulties Upgrading to Fluent NHibernate RC 1

I stumbled across Jason Dentler’s post on the problems he had upgrading his code to the Release Candidate of Fluent NHibernate today, specifically regarding subclasses.   I ran into some difficulties with subclasses when upgrading to the RC myself, but I was using a table-per-class-hierarchy structure rather than table-per-subclass, so I thought I’d post my problem and solution as well.

In the RC, rather than defining your class hierarchy entirely in the mapping of the parent class, you create a SubclassMap for each of your subclasses.  The wiki wasn’t quite clear about what needed to go into my subclass mappings since, like Jason, my subclasses didn’t have any extra properties, just redefined behavior. Turns out to be pretty simple, actually, and I think it ends up clearer than the old method.  Using the old way, I had the following in my parent class map:

DiscriminateSubClassesOnColumn<int>(“intCampaignTypeID”, -1)
.SubClass<InspectionCampaign>(1, x => {} )
.SubClass<OBPCampaign>(2, x=> { });
DiscriminateSubClassesOnColumn<int>("intCampaignTypeID", -1)
                .SubClass<InspectionCampaign>(1, x => {} )
                .SubClass<OBPCampaign>(2, x=> { });

This is looks okay, but the empty lambdas are a bit confusing.  Using the new SubclassMap way of doing things, in the parent class map, I just call the following method in the constructor:


And in each subclass map, I call the following method in the constructor:


Much easier to understand!  The signature for this method threw me off a bit, since my discriminator column is actually an integer, but the DiscriminatorValue method only accepts a string.  FNH seems to handle the conversion, though, because it worked just fine.

Hopefully this will be helpful to someone, at least until the wiki gets updated!

2 thoughts on “Subclass Difficulties Upgrading to Fluent NHibernate RC 1

  1. colum


    thanks for that. i was implementing fluent subclasss for first time and old documentation really threw me. this helped show how easy it is, once you know!

    good to post it.


  2. Karega

    Could you please post example of Parent and Child Mapping class. I’m get Association reference unmapped error.

    Thanks in advance


Leave a Reply

Your email address will not be published. Required fields are marked *