FluentNHiberante union-subclass mapping does not generate table for base class

1.2k views Asked by At

I am trying to map following domain model using union-subclass strategy and FluentNHibernate. Here is how my classes look (with unneeded parts removed)

public class Benefit
{
}
public class Leave : Benefit
{
}
public class SeasonTicketLoan : Benefit
{
}

And here is my mapping code

public class BenefitMappings : ClassMap<Benefit>
{
    public BenefitMappings()
    {
        UseUnionSubclassForInheritanceMapping();
    }         
}

public class LeaveMappings : SubclassMap<Leave>
{
}

public class SeasonTicketLoanMappings : SubclassMap<SeasonTicketLoan>
{
}

When I generate a database script using SchemaExport for the above mapping, I get a table for Leave and another one for SeasonTicketLoan but none for Benefit. Am I missing anything here?

1

There are 1 answers

1
Radim Köhler On

...Am I missing anything here?

Yes, you are using mapping Table Per Concrete Class (TPC), Which is intended to create

  • separate table per each class and
  • NO table for parent.

To get really deep and clear understanding, you should read this comprehensive article:

Inheritance mapping strategies in Fluent Nhibernate

Where you can read:

Table Per Concrete Class (TPC)

In TPC inheritance, every class in an inheritance hierarchy will have its own table. The inheritance hierarchy masks the fact that there are several independent underlying tables representing each subtype.

code snippet extract:

// mapping of the  TPCBaseEntity base class
public class TPCBaseEntityMap : ClassMap<TPCBaseEntity>
{
    public TPCBaseEntityMap()
    {
          // indicates that this class is the base
          // one for the TPC inheritance strategy and that 
          // the values of its properties should
          // be united with the values of derived classes
          UseUnionSubclassForInheritanceMapping();

In case, we would like to have also table per base class(es), we need:

Table Per Type(TPT)

TPT is an inheritance described in the database with separate tables. Every table provides additional details that describe a new type based on another table which is that table’s parent.

again some mapping snippet extract:

// mapping of the TPTAnimal base class
public class TPTAnimalMap : ClassMap<TPTAnimal>
{
    public TPTAnimalMap()
    {
          // the name of the schema that stores the table corresponding to the type 
          Schema("dbo");
          // the name of the table corresponding to the type
          Table("TPT_Animal");

...
// mapping of the TPTHorse class 
public class TPTHorseMap : SubclassMap<TPTHorse>
{
    public TPTHorseMap()
    {
          // the name of the schema that stores the table corresponding to the type 
          Schema("dbo");
          // the name of the table corresponding to the type
          Table("TPT_Horse");