Ang3lFir3 – Life as a Code Poet

August 17, 2009

Updates to Subclass and JoinedSubclass mapping in Fluent nHibernate 1.0 RC

Filed under: .NET, C#, Fluent NHibernate, NHibernate — Tags: , , , , — ang3lfir3 @ 3:20 pm

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");
    }
  }

kick it on DotNetKicks.com

About these ads

9 Comments »

  1. [...] Table Per Subclass Inheritance Mapping with Fluent nHibernate Filed under: .NET, C#, Fluent NHibernate, NHibernate, ORM, Patterns — Tags: .NET, C#, Code, Fluent NHibernate, NHibernate, ORM, Patterns — ang3lfir3 @ 2:45 pm Note: Updates for Fluent nHibernate 1.0 RC can be read here [...]

    Pingback by Table Per Subclass Inheritance Mapping with Fluent nHibernate « Ang3lFir3 – Life as a Code Poet — August 27, 2009 @ 1:17 pm

  2. [...] Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate Filed under: .NET, C#, Fluent NHibernate, NHibernate, ORM, Patterns — Tags: .NET, C#, Fluent NHibernate, NHibernate, ORM, Patterns — ang3lfir3 @ 2:32 pm Note: Updates for Fluent nHibernate 1.0 RC can be read here [...]

    Pingback by Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate « Ang3lFir3 – Life as a Code Poet — August 27, 2009 @ 1:19 pm

  3. 2 questions:
    When you save an entity of type: BillingDetail:
    how can can he tell which type to put in BillingDetailsType.

    I’ve tried this example, on my project, and what it does it tries to insert the inherited class properies into the base class table… any ideas why ?

    Comment by Dani — September 17, 2009 @ 8:40 am

    • 1) Fluent Nhibernate uses the string value of the class name by default as the discriminator. If you wish to use some other string or an int you can use one of the provided method overloads/generic methods.

      2) Sounds to me like your Mappings are setup for Table-Per-ClassHierarchy mapping (ie you have a call to DiscriminateSubClassesOnColumn(“BillingDetailsType”) in your Base class mapping) and that your database is setup for Table-Per-SubClass mapping.

      This post makes the assumption you have read the previous other two posts that are linked.

      Comment by ang3lfir3 — September 18, 2009 @ 7:06 am

      • 10x for the reply.
        I did read both articles, they are very good.

        I’m using table(tablename)
        in my classes.

        (need to update the old posts to FNH 1.0 RTM, I think some changes apply)

        The DB is table per subclass, with some data stored in the “parent” table and the rest is in the subclass.

        it looks like it changed from 0.9 to 1.

        I can post the mapping if it helps.

        Comment by dani — September 18, 2009 @ 7:30 am

  4. @Dani

    If your DB is setup for Table-PerSubClass then you don’t need the call to DiscriminateSubClassesOnColumn(“”);

    I left the old posts with the old info since it did change. I have linked to this post which covers the the changes from pre-RC to RC

    Comment by ang3lfir3 — September 18, 2009 @ 10:30 am

    • @ang3lfir3

      “If your DB is setup for Table-PerSubClass then you don’t need the call to DiscriminateSubClassesOnColumn(“”);”

      If I am using fluent to generate my schema then this is a problem?

      Are you saying that fluent will check the db schema first?

      I am using table per subclass and I am battling to get a solution for fluent v1.0.0.594

      Comment by Sam — January 18, 2010 @ 11:23 pm

      • No FluentNH does not check the db schema before hand. I basicly saying that by default the FNH mapping is Table-Per-SubClass if you make use of the SubClassMap

        Comment by ang3lfir3 — January 19, 2010 @ 3:14 pm

  5. This article also has useful information about inheritance.

    http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat

    Comment by Nao — October 24, 2012 @ 5:15 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:

WordPress.com Logo

You are commenting using your WordPress.com 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

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: