Could not synchronize database state with session
org.hibernate.StaleObjectStateException:
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.introns.model.goods.NetGoods#402880d31fa720c1011fa72605690001]
说明操作的对象应用了乐观锁;version
错误原因:当在进行操作时,当前version版本与DB中version版本不一致;
A:另外有人对此记录进行了相似操作;
B:自己操作有误:
例如通过编辑页面来修改一条记录时:传回来的form对象中version为空,然后又指导些form中的值copy到相应的实体对象中;--此时此实体对象中的version也为空;当update时,version不一致就会出错;
解决: A:用form传数据时,把version也传\过来;
B:从DB中把version查出来,赋给实体对象;再更新;
Row was updated or deleted by another transaction
timestamp的字段一定要有值,并且是日期值,否则当hibernate更新这个字段没有值的那条记录时,会出现如下错误
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.test.admin.domain.user#1]
当选择assigned生成器时,除非有一个version或timestamp属性,或者你定义了 Interceptor.isUnsaved(),否则需要让Hiberante使用 unsaved-value="undefined",强制Hibernatet查询数据库来确定一个实例是瞬时的(transient) 还是脱管的(detached)。
<timestamp name="lastUpdate" column="user_lastupdate" generated="never" />
never指不从数据库中产生。
Hibernate 疑难异常及处理
1、a different object with the same identifier value was already associated with the session。
错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
解决方法一:session.clean()
PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。
解决方法二:session.refresh(object)
PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
解决方法三:session.merge(object)
PS:Hibernate里面自带的方法,推荐使用。
2、Found two representations of same collection
错误原因:见1。
解决方法:session.merge(object)
以上两中异常经常出现在一对多映射和多对多映射中
分享到:
相关推荐
#hibernate.transaction.manager_lookup_class org.hibernate.transaction.WebSphereTransactionManagerLookup #hibernate.transaction.manager_lookup_class org.hibernate.transaction....
*DELETED**DELETED**DELETED*
In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have ...
deleted deleted deleted deleted deleted
Entities may have been modified or deleted since entities were loaded. See ...
Empowered by the new unique data recovery technologies, it is the most comprehensive data recovery solution for recovery files from FAT12/16/32, NTFS, NTFS5 (created or updated by Windows 2000/XP/...
相信剖析MATLAB工程应用技术。M文件均经过验证,非常好用。
The calculation was triggered every time a rating was created or deleted. We used serverside JavaScript to create rating objects to test out our behavior but there wasn't an interface available ...
Dont Download, it will be deleted
SQL Inserted和deleted详解
Updated the PSP0 tutorial to better explain how to mark an assignment complete. • The PSP2 was updated to accurately describe how to complete defect planning with the SEI Access student workbook....
To be deleted. How to delete this file?
They can be saved to another drive with a few mouseclicks (see Searching deleted files for step by step instructions). Recover4all?only lists folders that contain deleted files. Normal nondeleted ...
当使用Laravel SoftDeletes特征时,此包还处理deleted_by列。 正在安装 此软件包要求Laravel 5.2或更高版本在PHP 5.6或更高版本上运行。 可以使用composer安装此软件包: composer require wildside/userstamps ...
Property 'common.setting2' was deleted Property 'common.setting3' was updated. From 'true' to complex value Property 'common.setting6.ops' was added with value: 'vops' Property 'common.setting4' was ...
7.6 You agree, so long as you have not entirely deleted Vagaa from your computer, not to take any action, including downloading other software, to disable or block the display of advertising by the ...
Most objects in MS Word templates GN recolorados can be effortlessly moved or deleted. Type or paste your own text, add or remove text boxes and change the font if you wish. Replace stock images with...
An Integrated Approach to Recovering Deleted Files from NAND Flash Data