我四处查找了很多关于如何映射子对象的示例,但我缺少了一些东西。我无法使以下映射工作
表1:
ORDERID RAW No
HISTORYID RAW No
映射:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping auto-import="false" xmlns="urn:nhibernate-mapping-2.2">
<class name="Order" lazy="false" table="Orders" polymorphism="explicit" dynamic-insert="true">
<id name="OrderId" column="OrderId" type="Guid">
<generator class="GuidGenerator" />
</id>
<bag name="OrderHistoryBag" lazy="false" table="OrderHistory" cascade="none">
<key column="HistoryId" />
<one-to-many class="OrderHistory" not-found="ignore" />
</bag>
</class>
</hibernate-mapping>
类属性:
public virtual IList<OrderHistory> OrderHistoryBag { get; set; }
public virtual Collection<OrderHistory> OrderHistory { get; set; }
表2:
HISTORYSEQ NUMBER(6,0) No
HISTORYID RAW Yes
映射:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="OrderHistory" lazy="false" table="OrderHistory" polymorphism="explicit">
<id name="HistorySequence" column="HistorySeq" type="Int32">
<generator class="sequence">
<param name="sequence">S_Hist</param>
</generator>
</id>
<many-to-one name="Order" class="Order" column="HistoryId" not-null="true" cascade="none" lazy="false" />
</class>
</hibernate-mapping>
类别属性:
public virtual Order Order { get; set; }
除了OrderHistoryBag及其关联的OrderHistory集合始终是一个空集合之外,所有内容都编译并运行良好。
我想小故事是,我试图将父类中的HistoryId映射到子类中的HistoryId,这两个都不是实体上的主键。我的NHibernate程序集是v1.2.1.400(不要问)。
您需要指定用于使用属性ref 联接到historyItems的属性
<property name="HistoryId" />
<bag name="OrderHistoryBag" lazy="false" table="OrderHistory" cascade="none">
<key column="HistoryId" property-ref="HistoryId"/>
<one-to-many class="OrderHistory" not-found="ignore" />
</bag>