简体   繁体   English

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

[英]NHibernate StaleObjectStateException when using version optimistic locking for an object with a one-to-many relationship

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). 我正在按照Billy McCafferty的最佳做法文章中的建议使用每个请求的会话使用NHibernate和ASP.Net(对不起,我不能包含链接)。 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. 我已经成功地将其与版本乐观锁定一起使用,将更新的对象保存在HTTP会话对象中,并使用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). 我使用了NHibernate手册(第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. 但是,当加载到一个请求中,保存在HTTP会话中并使用SaveOrUpdate重新附加到后续请求中时,刷新NHibernate会话时会出现StaleObjectException。 This happens even if no changes are made to the child object collection. 即使未对子对象集合进行任何更改,也会发生这种情况。

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. 对父对象的属性所做的更改将保存到数据库,因此NHibernate似乎试图两次更新该对象。 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. 我怀疑这与映射中的层叠选项有关,但是为了使父子关系正常工作,这些是必需的。

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. 请有人帮忙。

对于其他发现此问题的人来说,该问题似乎已在nhibernate 2.1中解决,因此您应该进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nhibernate:无法映射一对多关系 - Nhibernate: Trouble mapping a one-to-many relationship NHibernate-基于类型列的热切负载一对多关系 - NHibernate - Eager load one-to-many relationship on the basis of type column NHibernate一对多删除不级联 - NHibernate One-To-Many Delete Not Cascading 一对多关系不编辑子表 - One-To-Many Relationship not editing Child table 创建动作方法中的一对多关系 - One-to-many relationship in the create action method 映射nhibernate xml多对一关系 - mapping nhibernate xml many-to-one relationship 如何使用Entity Framework从具有一对多关系的两个表中选择所有相关数据? - How to select all related data from two tables having one-to-many relationship using Entity Framework? 使用实体框架在 ASP.NET MVC 中为具有一对多关系的数据库播种 - Seeding database with a one-to-many relationship in ASP.NET MVC using Entity Framework 使用SQL Server数据存储的实体框架为一对多关系建模 - Entity Framework using SQL Server datastore modelling a one-to-many relationship 使用列中的定界符对一对多关系表进行单个SQL Select-Oracle - Single SQL Select for one-to-many relationship tables using delimiter in column - Oracle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM