NHibernate父/子映射不会填充包

时间:2012-08-21 20:13:24

标签: c# nhibernate nhibernate-mapping

我看了很多,发现了很多关于如何映射孩子的例子,但我遗漏了一些东西。我不能让后续的映射工作

表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及其关联的OrderHistorycollection始终是空集合外,所有内容都编译并运行正常。

我想短篇小说是我试图将父类中的HistoryId映射到子类中的HistoryId,这两个类都不是实体的主键。 我的NHibernate程序集是v1.2.1.400(不要问)。

1 个答案:

答案 0 :(得分:1)

您需要使用property-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>