NHibernate错误 - 在刷新之前保存瞬态实例

时间:2009-11-03 12:55:36

标签: .net nhibernate

我已经成功编写了一个系统,其中包含带有子订单行的订单,使用订单中的级联更新来保存订单行。在下一次迭代中,我想将订单行与一个发货类相关联。这似乎很简单 - 将一个可以为空的DespatchID列添加到订单行表中,作为Despatch表的foriegn键。但是,当我将其添加到映射并使用未在订单行上设置的调度保存订单对象时,我收到错误“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例”。

如果我删除了订单行和发货单之间的关联,则保存正常。我只能假设错误是由于它试图保存不存在的发送而引起的。

以下是映射(订单行类称为OrderProductAmount):

  <class name="NHS.WebTeam.PandemicFluDistribution.Order, NHS.WebTeam.PandemicFluDistribution" table="[Order]" lazy="false" optimistic-lock="version" where="Deleted=0" >
    <id name="ID" type="Int32" column="OrderID" unsaved-value="0">
      <generator class="hilo">
        <param name="table">NHibernateHiLo</param>
        <param name="column">NextValue</param>
        <param name="max_lo">100</param>
      </generator>
    </id>

    <version column="version" name="Version"/>
    <property name="Deleted" column="Deleted" />

    <property name="DateEntered"></property>
    <property name="RequiredDeliveryDate"></property>

    <many-to-one name="Practice" column="PracticeID"></many-to-one>

    <set name="OrderProductAmounts" access="field.camelcase-underscore" inverse="true" cascade="all-delete-orphan" lazy="true">
      <key column="OrderID"></key>
      <one-to-many class="NHS.WebTeam.PandemicFluDistribution.OrderProductAmount, PandemicFluDistribution" />
    </set>

  </class>

  <class name="NHS.WebTeam.PandemicFluDistribution.OrderProductAmount, NHS.WebTeam.PandemicFluDistribution" table="OrderProductAmount" lazy="false" optimistic-lock="version" where="Deleted=0" >
    <id name="ID" type="Int32" column="OrderProductAmountID" unsaved-value="0">
      <generator class="hilo">
        <param name="table">NHibernateHiLo</param>
        <param name="column">NextValue</param>
        <param name="max_lo">100</param>
      </generator>
    </id>

    <version column="version" name="Version"/>
    <property name="Deleted" column="Deleted" />

    <many-to-one name="Order" column="OrderID"></many-to-one>
    <many-to-one name="ProductAmount" column="ProductAmountID"></many-to-one>
    <many-to-one name="Despatch" column="DespatchID" cascade="none" not-null="false"></many-to-one>
  </class>

  <class name="NHS.WebTeam.PandemicFluDistribution.Despatch, NHS.WebTeam.PandemicFluDistribution" table="Despatch" lazy="false" optimistic-lock="version" where="Deleted=0" >
    <id name="ID" type="Int32" column="DespatchID" unsaved-value="0">
      <generator class="hilo">
        <param name="table">NHibernateHiLo</param>
        <param name="column">NextValue</param>
        <param name="max_lo">100</param>
      </generator>
    </id>

    <version column="version" name="Version"/>
    <property name="Deleted" column="Deleted" />

    <property name="DateDespatched"></property>
    <property name="RequiredDeliveryDate"></property>

    <many-to-one name="Practice" column="PracticeID"></many-to-one>

    <set name="OrderProductAmounts" access="field.camelcase-underscore" inverse="true" cascade="none" lazy="true">
      <key column="DespatchID"></key>
      <one-to-many class="NHS.WebTeam.PandemicFluDistribution.OrderProductAmount, PandemicFluDistribution" />
    </set>

  </class>

创建订单的代码基本上是这样的:

Dim practice = ... get relevant Practice ...
Dim productAmount = ... get relevant ProductAmount ...
Dim newOrder as Order = new Order(practice)
newOrder.AddProductAmount(new OrderProductAmount(newOrder, productAmount)
OrderDAO.Save(newOrder)

有人有什么想法吗?

1 个答案:

答案 0 :(得分:21)

正如上面的评论所暗示的那样(非常感谢),有些事情对于Despatch来说听起来不对。事实证明,Despatch不是问题所在。虽然由于某些原因它在我添加之前有效。

我通过向订单的OrderProductAmount链接添加了一个cascade =“all”解决了这个问题:

<many-to-one name="Order" column="OrderID" cascade="all"></many-to-one>

抛出的错误是订单在保存OrderProductAmount时仍然是暂时的。这是非常令人困惑的,因为它是Order而不是我正在保存的OrderProductAmount - OrderProductAmounts只能通过Order中的级联保存。

所以,如果有人知道为什么会这样,我有兴趣知道。