我有一个类概念上看起来类似的类层次结构:
也就是说,有一个抽象基类(Relation
)和几个派生类。在实践中,Customer
和Supplier
共享了大量代码,因此我将共性重构为抽象类BusinessContact
。现在实际的类层次结构如下所示:
或代码:
public abstract class Relation
{
public virtual int Id { get; set; }
}
public class ContactPerson : Relation
{
public virtual string PhoneNumber { get; set; }
}
public abstract class BusinessContact : Relation
{
public virtual string Name { get; set; }
}
public class Customer : BusinessContact
{
public virtual string CustomerNumber { get; set; }
}
public class Supplier : BusinessContact
{
public virtual string SupplierNumber { get; set; }
}
我想在NHibernate中使用join-subclasses将此层次结构映射到四个表(Relation
,ContactPerson
,Customer
和Supplier
),使用mapping-by -code(ModelMapper
)。我的映射看起来像这样:
var mapper = new ModelMapper();
mapper.Class<Relation>(map =>
{
map.Id(x => x.Id, id => id.Generator(Generators.Native));
});
mapper.JoinedSubclass<ContactPerson>(map =>
{
map.Key(key => key.Column("Id"));
map.Property(x => x.PhoneNumber);
});
mapper.JoinedSubclass<Customer>(map =>
{
map.Key(key => key.Column("Id"));
map.Property(x => x.Name);
map.Property(x => x.CustomerNumber);
});
mapper.JoinedSubclass<Supplier>(map =>
{
map.Key(key => key.Column("Id"));
map.Property(x => x.Name);
map.Property(x => x.SupplierNumber);
});
但是,只要我尝试将映射添加到配置中,我就会遇到异常:
NHibernate.MappingException: Cannot extend unmapped class: BusinessContact
我基本上明白为什么会这样。生成的映射如下所示:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" assembly="JoinedSubClassMapping" xmlns="urn:nhib ernate-mapping-2.2">
<class name="Relation" abstract="true">
<id name="Id" type="Int32">
<generator class="native" />
</id>
</class>
<joined-subclass name="ContactPerson" extends="Relation">
<key column="Id" />
<property name="PhoneNumber" />
</joined-subclass>
<joined-subclass name="Customer" extends="BusinessContact">
<key column="Id" />
<property name="CustomerNumber" />
</joined-subclass>
<joined-subclass name="Supplier" extends="BusinessContact">
<key column="Id" />
<property name="SupplierNumber" />
</joined-subclass>
</hibernate-mapping>
Customer
和Supplier
在BusinessContact
属性中定义extends
,就好像BusinessContact
是模型中的“普通”实体一样。因为BusinessContact
没有映射,所以失败了,或者当然。请注意,“名称”属性(在BusinessContact
中定义)也不会出现在映射中。
我想要的映射是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" assembly="JoinedSubClassMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="Relation" abstract="true">
<id name="Id" type="Int32">
<generator class="native" />
</id>
</class>
<joined-subclass name="ContactPerson" extends="Relation">
<key column="Id" />
<property name="PhoneNumber" />
</joined-subclass>
<joined-subclass name="Customer" extends="Relation">
<key column="Id" />
<property name="CustomerNumber" />
<property name="Name" />
</joined-subclass>
<joined-subclass name="Supplier" extends="Relation">
<key column="Id" />
<property name="SupplierNumber" />
<property name="Name" />
</joined-subclass>
</hibernate-mapping>
即,make Supplier
和Customer
扩展Relation并包含(否则未映射)BusinessContact
类的所有映射属性。
我怎样才能做到这一点?
答案 0 :(得分:2)
这是一个解决方案:
class MyInspector : ExplicitlyDeclaredModel {
public override bool IsEntity(Type type) {
if (type == typeof (BusinessContact))
return false;
return base.IsEntity(type);
}
}
var mapper = new ModelMapper(new MyInspector());