使用 Fluent NHibernate 进行继承映射 [英] Inheritance Mapping with Fluent NHibernate
问题描述
鉴于以下场景,我想使用 Fluent NHibernate 将类型层次结构映射到数据库架构.
我使用的是 NHibernate 2.0
<小时>类型层次结构
公共抽象类Item{公共虚拟 int ItemId { 获取;放;}公共虚拟字符串 ItemType { 获取;放;}公共虚拟字符串 FieldA { get;放;}}公共抽象类 SubItem : Item{公共虚拟字符串 FieldB { get;放;}}公共类 ConcreteItemX : SubItem{公共虚拟字符串 FieldC { get;放;}}公共类 ConcreteItemY : 项目{公共虚拟字符串 FieldD { get;放;}}
Item
和 SubItem
类是抽象的.
数据库架构
<前>+--------------+ +---------------+ +---------------+|项目 ||混凝土项目X ||混凝土项目Y |+==========+ +================+ +================+|商品编号 ||商品编号 ||商品编号 ||项目类型 ||场C ||领域D ||领域A |+--------------+ +--------------+|场B |+-----------+ItemType
字段决定了具体的类型.
ConcreteItemX
表中的每条记录在Item
表中都有一个对应的记录;ConcreteItemY
表也是如此.
ConcreteItemY
,FieldB
始终为 null.
映射(到目前为止)
public class ItemMap : ClassMap- {公共 ItemMap(){WithTable("项目");Id(x => x.ItemId, "ItemId");Map(x => x.FieldA, "FieldA");JoinedSubClass
("ItemId", MapConcreteItemX);JoinedSubClass("ItemId", MapConcreteItemY);}private static void MapConcreteItemX(JoinedSubClassPart part){part.WithTableName("ConcreteItemX");part.Map(x => x.FieldC, "FieldC");}private static void MapConcreteItemY(JoinedSubClassPart part){part.WithTableName("ConcreteItemX");part.Map(x => x.FieldD, "FieldD");}}
FieldB
未映射.
问题
如何使用 Fluent NHibernate 映射 SubItem
类的 FieldB
属性?
有什么方法可以使用 ItemType
字段来利用 DiscriminateSubClassesOnColumn
吗?
附录
我能够使用 hbm.xml 文件实现所需的结果:
<id name="ItemId" type="Int32" column="ItemId"><generator class="native"/></id><discriminator column="ItemType" type="string"/><属性名称=字段A"列=字段A"/><subclass name="ConcreteItemX" discriminator-value="ConcreteItemX"><!-- 请注意此处的 FieldB 映射--><属性名称=FieldB"列=FieldB"/><join table="ConcreteItemX"><key column="ItemId"/><属性名称=FieldC"列=FieldC"/></加入></子类><subclass name="ConcreteItemY" discriminator-value="ConcreteItemY"><join table="ConcreteItemY"><key column="ItemId"/><属性名称=FieldD"列=FieldD"/></加入></子类></class>
如何使用 Fluent NHibernate 完成上述映射?
是否可以使用 Fluent NHibernate 将 table-per-class-hierarchy 与 table-per-subclass 混合使用?
我知道这真的很旧,但现在设置 fluent 以生成您最初想要的精确映射非常简单.由于我在搜索答案时遇到了这篇文章,所以我想我会发布它.
您只需为基类创建 ClassMap,而无需引用您的子类:
public class ItemMap : ClassMap- {公共 ItemMap(){this.Table("项目");this.DiscriminateSubClassesOnColumn("ItemType");this.Id(x => x.ItemId, "ItemId");this.Map(x => x.FieldA, "FieldA");}}
然后像这样映射你的抽象子类:
公共类SubItemMap:SubclassMap{公共 SubItemMap(){this.Map(x => x.FieldB);}}
然后像这样映射你的具体子类:
public class ConcreteItemXMap : SubclassMap{公共 ConcretItemXMap(){this.Join("ConcreteItemX", x =>{x.KeyColumn("ItemID");x.Map("FieldC")});}}
希望这可以帮助其他人使用 fluent 寻找这种类型的映射.
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?
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:
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
this.Table("Item");
this.DiscriminateSubClassesOnColumn("ItemType");
this.Id(x => x.ItemId, "ItemId");
this.Map(x => x.FieldA, "FieldA");
}
}
Then map your abstract subclass like this:
public class SubItemMap: SubclassMap<SubItemMap>
{
public SubItemMap()
{
this.Map(x => x.FieldB);
}
}
Then map your concrete subclasses like so:
public class ConcreteItemXMap : SubclassMap<ConcreteItemX>
{
public ConcretItemXMap()
{
this.Join("ConcreteItemX", x =>
{
x.KeyColumn("ItemID");
x.Map("FieldC")
});
}
}
Hopefully this helps somebody else looking for this type of mapping with fluent.
这篇关于使用 Fluent NHibernate 进行继承映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!