当对具有一对多关系的对象使用版本乐观锁定时,NHibernate StaleObjectStateException

时间:2022-03-07 00:09:19

I am using NHibernate and ASP.Net using a session per request as suggested in the best practices article by Billy McCafferty (sorry, I cannot include the link). I have used this successfully with version optimistic locking, saving updated objects in the HTTP Session object and reattaching to the NHibernate session using the SaveOrUpdate method.

我正在使用NHibernate和ASP.Net使用每个请求的会话,如Billy McCafferty的最佳实践文章中所建议的(抱歉,我不能包含该链接)。我成功地使用了版本乐观锁定,在HTTP Session对象中保存更新的对象,并使用SaveOrUpdate方法重新连接到NHibernate会话。

However, my latest page requires the update of a collection of child objects. I used the method suggested in the parent child example of the NHibernate manual (Chapter 17). This works when loaded and saved in a single request. However, when loaded in one request, saved in the HTTP Session, and reattached in a subsequent request using SaveOrUpdate, I get a StaleObjectException when flushing the NHibernate session. This happens even if no changes are made to the child object collection.

但是,我的最新页面需要更新子对象的集合。我使用了NHibernate手册(第17章)的父子示例中建议的方法。这在加载并保存在单个请求中时有效。但是,当在一个请求中加载,保存在HTTP会话中,并使用SaveOrUpdate在后续请求中重新附加时,在刷新NHibernate会话时,我得到一个StaleObjectException。即使未对子对象集合进行任何更改,也会发生这种情况。

Changes that are made to the parent object's properties are saved to the database, so it would appear that NHibernate is trying to update the object twice. I suspect that this is something to do with the cascade options in the mapping, but these are required in order to get the parent/child relationship working correctly.

对父对象的属性所做的更改将保存到数据库中,因此NHibernate会尝试两次更新对象。我怀疑这与映射中的级联选项有关,但这些是为了使父/子关系正常工作所必需的。

Here are my mapping files:

这是我的映射文件:

Parent Class Mapping

父类映射

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHS.WebTeam.PharmacyFirst.Medication, PharmacyFirst" table="Medication" lazy="false" optimistic-lock="version" where="Deleted=0">
    <id name="ID" column="Medication_ID" unsaved-value="0">
      <generator class="identity" />
    </id>

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

    <property name="Name" column="Name" />

    <bag name="Prices" access="field.camelcase-underscore" lazy="false" inverse="true" cascade="all">
      <key column="Medication_ID"/>
      <one-to-many class="NHS.WebTeam.PharmacyFirst.MedicationPrice, PharmacyFirst" />
    </bag>

  </class>
</hibernate-mapping>

Child Class Mapping

子类映射

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="NHS.WebTeam.PharmacyFirst.MedicationPrice, PharmacyFirst" table="Medication_Price" lazy="false" optimistic-lock="version" where="Deleted=0">
    <id name="ID" column="Medication_ID" unsaved-value="0">
      <generator class="identity" />
    </id>

    <many-to-one name="Medication" column="medication_id" not-null="true" cascade="none"/>
    <property name="DateFrom" column="Date_From" />
    <property name="Price" column="Price" />

  </class>
</hibernate-mapping>

Please can somebody help.

请有人帮忙。

1 个解决方案

#1


For anyone else finding this, it appears the issue has been fixed in nhibernate 2.1, so you should just update.

对于其他发现这一点的人来说,似乎问题已在nhibernate 2.1中得到修复,所以你应该更新。

#1


For anyone else finding this, it appears the issue has been fixed in nhibernate 2.1, so you should just update.

对于其他发现这一点的人来说,似乎问题已在nhibernate 2.1中得到修复,所以你应该更新。