This post is the second post in a multi-part series of examples for NHibernate‘s Map by Code feature. Code in this series has been tested with NHibernate’s latest 4.0 as well as 3.3.3.
- Part One
- Many To Many
- Inverse Bag
- Part Two (you are here)
- JoinedSubClassMapping vs SubclassMapping (Discriminators) vs UnionSubclassMapping
- Part Three
- ForeignKey “none”
JoinedSubClassMapping vs SubclassMapping (Discriminators) vs UnionSubclassMapping
For this example we will continue to use our teachers/students and add some classroom supplies to the mix. Let’s take a look at our new domain entities – art supply and cleaning supply. Both of these are supplies so we have a base supply that both are derived from.
All three of these mappings distinctly different table structures that can be used for different purposes. First you will notice that for our discriminated classes there is only one table (Table per Hierarchy). NHibernate uses the discriminator column to determine concrete type when inserting and retrieving entities in this mapping strategy.
In the joined mapping strategy NHibernate models this as a Table per Type and performs a join when retrieving entities.
In the unioned mapping strategy, NHibernate creates a Table per Concrete Class and will automatically union all results together when performing a query over the BaseSupplyUnioned class and return instances of the concrete classes.