我在使用 NHibernate 映射时遇到问题,我不确定我是否犯了一个菜鸟错误,或者我是否遇到了 NHibernate 的限制
我有三个域对象与关联的映射 hbm.xml 文件
Person
SpecialPerson
PersonCategory
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Person"
table="Person">
<id name="Id" column="PersonID">
<generator class="native" />
</id>
<property name="Name" />
</class>
</hibernate-mapping>
SpecialPerson 派生自 Person
public class SpecialPerson : Person
{
public virtual string MagicString { get; set; }
public virtual PersonCategory PersonCategory { get; set; }
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<joined-subclass name="SpecialPerson"
extends="Person">
<key column="PersonID" />
<property name="MagicString" />
<many-to-one name="PersonCategory" column="PersonCategoryID" cascade="save-update" />
</joined-subclass>
</hibernate-mapping>
并且 PersonCategory 拥有一组 SpecialPersons <== 这就是让我感到悲伤的原因
public class PersonCategory
{
public virtual int Id { get; set; }
public virtual ICollection<SpecialPerson> Persons { get; set; }
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="PersonCategory"
table="PersonCategory">
<id name="Id" column="PersonCategoryID">
<generator class="native" />
</id>
<set name="Persons" table="SpecialPerson" inverse="true">
<key column="PersonCategoryID" />
<one-to-many class="SpecialPerson"/>
</set>
</class>
</hibernate-mapping>
当我尝试创建会话时,我得到一个 NHibernate.MappingException 说“关联引用未映射的类:SpecialPerson”
要么我没有使用正确的语法,因为我不应该指定 table 属性,但是当我尝试引用派生类型或
NHibernate 不允许对象保存派生的集合时类型,除非该派生类型根据具体的类继承映射策略映射到表中。任何人都可以启发我吗?