Given the following scenario, I want map the type hierarchy to the database schema using Fluent NHibernate.
I am using NHibernate 2.0
Type Hierarchy
public abstract class Item
{
public virtual int ItemId { get; set; }
public virtual string ItemType { get; set; }
public virtual string FieldA { get; set; }
}
public abstract class SubItem : Item
{
public virtual string FieldB { get; set; }
}
public class ConcreteItemX : SubItem
{
public virtual string FieldC { get; set; }
}
public class ConcreteItemY : Item
{
public virtual string FieldD { get; set; }
}
The Item
and SubItem
classes are abstract.
Database Schema
+----------+ +---------------+ +---------------+ | Item | | ConcreteItemX | | ConcreteItemY | +==========+ +===============+ +===============+ | ItemId | | ItemId | | ItemId | | ItemType | | FieldC | | FieldD | | FieldA | +---------------+ +---------------+ | FieldB | +----------+
The ItemType
field determines the concrete type.
Each record in the ConcreteItemX
table has a single corresponding record in the Item
table; likewise for the ConcreteItemY
table.
FieldB
is always null if the item type is ConcreteItemY
.
The Mapping (so far)
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
WithTable("Item");
Id(x => x.ItemId, "ItemId");
Map(x => x.FieldA, "FieldA");
JoinedSubClass<ConcreteItemX>("ItemId", MapConcreteItemX);
JoinedSubClass<ConcreteItemY>("ItemId", MapConcreteItemY);
}
private static void MapConcreteItemX(JoinedSubClassPart<ConcreteItemX> part)
{
part.WithTableName("ConcreteItemX");
part.Map(x => x.FieldC, "FieldC");
}
private static void MapConcreteItemY(JoinedSubClassPart<ConcreteItemY> part)
{
part.WithTableName("ConcreteItemX");
part.Map(x => x.FieldD, "FieldD");
}
}
FieldB
is not mapped.
The Question
How do I map the FieldB
property of the SubItem
class using Fluent NHibernate?
Is there any way I can leverage DiscriminateSubClassesOnColumn
using the ItemType
field?
Addendum
I am able to achieve the desired result using an hbm.xml file:
<class name="Item" table="Item">
<id name="ItemId" type="Int32" column="ItemId">
<generator class="native"/>
</id>
<discriminator column="ItemType" type="string"/>
<property name="FieldA" column="FieldA"/>
<subclass name="ConcreteItemX" discriminator-value="ConcreteItemX">
<!-- Note the FieldB mapping here -->
<property name="FieldB" column="FieldB"/>
<join table="ConcreteItemX">
<key column="ItemId"/>
<property name="FieldC" column="FieldC"/>
</join>
</subclass>
<subclass name="ConcreteItemY" discriminator-value="ConcreteItemY">
<join table="ConcreteItemY">
<key column="ItemId"/>
<property name="FieldD" column="FieldD"/>
</join>
</subclass>
</class>
How do I accomplish the above mapping using Fluent NHibernate?
Is it possible to mix table-per-class-hierarchy with table-per-subclass using Fluent NHibernate?
This is how I resolved my inheritance problem:
You can then add this to your superclass map constructor:
I know this is really old, but it is now pretty simple to set up fluent to generate the exact mapping you initially desired. Since I came across this post when searching for the answer, I thought I'd post it.
You just create your ClassMap for the base class without any reference to your subclasses:
Then map your abstract subclass like this:
Then map your concrete subclasses like so:
Hopefully this helps somebody else looking for this type of mapping with fluent.
The line of code:
if (part.GetType().BaseType.Name == "JoinedSubClassPart1")
can be rewritten as follows:Well, I'm not sure that it's quite right, but it might work... If anyone can do this more cleanly, I'd love to see it (seriously, I would; this is an interesting problem).
Using the exact class definitions you gave, here are the mappings:
Those mappings produce two hbm.xml files like so (some extraneous data removed for clarity):
It's ugly, but it looks like it might generate a usable mapping file and it's Fluent! :/ You might be able to tweak the idea some more to get exactly what you want.