我有2个类,Forecast
和Cost
,它们之间定义了多对多关系。我们现在在链接表中添加了“订单”列(因此,我们的客户可以在Forecast
内指定Cost
的订单)。为了在我们的映射中反映出这一点,我们创建了一个新实体ForecastCost
并将Forecast
和Cost
之间的多对多替换为Forecast
1- * ForecastCost
* -1 Cost
。返回“对象引用未保存的瞬态实例”
的映射是这样的:
Forecast.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="FinRep" assembly="FinRep">
<class name="Forecast" table="FORECAST" polymorphism="implicit">
<id name="Id" column="PKFORECAST" type="long" access="field.camelcase" unsaved-value="0"><!-- ... --></id>
<!-- ... -->
<bag name="KostComponenten" table="FORECASTCOST" access="field.camelcase" lazy="false" cascade="save-update">
<key column="FKFRBFORECAST"></key>
<one-to-many class="ForecastCost" />
</bag>
</class>
</hibernate-mapping>
ForecastCost.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="FinRep" assembly="FinRep">
<class name="ForecastCost" table="FORECASTCOST">
<composite-id name="ForecastCostId" class="FinRep.ForecastCostKey, FinRep">
<key-many-to-one name="Forecast" column="FKFORECAST" lazy="false" class="FinRep.Forecast, FinRep" />
<key-many-to-one name="Cost" column="FKCOST" lazy="false" class="FinRep.Cost, FinRep" />
</composite-id>
<property name="Order" column="ORDER" />
</class>
</hibernate-mapping>
首先, '级联' Forecast
映射中的包上没有设置属性,所以当我试图保存Forecast
有一些Cost
链接到它,我会(理所当然地)得到一个错误,说有关的ForecastCost
对象不能从数据库中检索。我认为加入'cascade'属性会解决这个问题,但是现在当我尝试保存Forecast
#1(例如)时,我得到一个异常,说明类型为= Forecast entity = 1的object references an unsaved transient instance - save the transient instance before flushing
。
我认为正在发生的事情是,它试图挽救预测如何,并通过这样做,也试图坚持链接到它所有的ForecastCost
/Cost
对象,但ForecastCost
对象也有一个链接回Forecast
实体我们正在保存(但还没有保存,因此是暂时的),所以它出错了。
我应该怎么做才能解决这个问题?
东西我刚刚发现 - 使用时,我只能得到错误'SaveOrUpdateCopy'。当使用'SaveOrUpdate'时,一切正常。 – efdee