.net NHibernate 错误 - 在刷新之前保存瞬态实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1667177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NHibernate Error - Save the Transient Instance Before Flushing
提问by Richard Bramley
I had successfully written a system that contained an order with child order lines, using cascade updates from the order to save the order lines. In the next iteration I want to associate the order lines with a despatch class. This seemed quite straight forward - add a nullable DespatchID column to the order line table as a foriegn key to the Despatch table. However, when I add this to the mapping and save the order object with the despatches not set on the order lines, I get the error "object references an unsaved transient instance - save the transient instance before flushing".
我已经成功地编写了一个包含带有子订单行的订单的系统,使用订单的级联更新来保存订单行。在下一次迭代中,我想将订单行与发货类相关联。这看起来很简单——向订单行表添加一个可为空的 DespatchID 列作为 Despatch 表的外键。但是,当我将其添加到映射中并保存订单对象时未在订单行上设置发货单时,出现错误“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例”。
If I remove the association between the order line and the despatch, it saves OK. I can only presume that the error is caused because it is trying to save the despatch, which does not exist.
如果我删除订单行和发货之间的关联,则保存正常。我只能假设错误是因为它试图保存不存在的快递。
Here are the mappings (the order lines class is called OrderProductAmount):
以下是映射(订单行类称为 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>
The code to create the order is essentially this:
创建订单的代码本质上是这样的:
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)
Does anybody have any ideas?
有人有任何想法吗?
回答by Richard Bramley
As implied by the comments above (for which a big thanks), there was something that didn't sound right about the Despatch. And, as it turned out, the Despatch was not the problem. Although for some reason it worked before I added it.
正如上面的评论所暗示的(非常感谢),Despatch 听起来有些不对劲。而且,事实证明,Despatch 不是问题所在。尽管出于某种原因它在我添加它之前起作用了。
I resolved the problem by adding a cascade="all" to the OrderProductAmount link to the Order:
我通过在 OrderProductAmount 链接中添加一个 cascade="all" 来解决这个问题:
<many-to-one name="Order" column="OrderID" cascade="all"></many-to-one>
The error that was thrown was that the Order was still transient when the OrderProductAmount was being saved. This is very confusing since it is the Order and not the OrderProductAmount that I am saving - the OrderProductAmounts are only saved through a cascade from the Order.
抛出的错误是在保存 OrderProductAmount 时订单仍然是暂时的。这非常令人困惑,因为我保存的是 Order 而不是 OrderProductAmount - OrderProductAmounts 仅通过订单的级联保存。
So, if anybody has any ideas why this might be, I'd be interested to know.
所以,如果有人知道为什么会这样,我很想知道。

