我正在为我的应用程序使用 hibernate、spring、struts 框架。
在我的应用程序中,每个表都有一个名为“版本”的字段,用于跟踪任何记录的更新。
每当我更新版本为 0 的国家/地区表的现有记录时,它都可以正常工作并更新记录,将版本字段更新为 1。
但是每当我尝试更新版本 1 记录时,都会出现如下错误:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [com.sufalam.business.marketing.model.bean.Country] with identifier [3]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.company.business.marketing.model.bean.Country#3]
有什么办法可以解决吗?
最佳答案
Hibernate的版本栏允许你实现optimistic concurrency control 。
每次更新对象时,Hibernate 都会检查数据库中存储的版本值是否与对象中的版本值相同。如果两者不同,则抛出 StaleObjectStateException,这意味着其他人在当前 session 加载、编辑和存储该对象的同时更新了该对象。
您必须确保对象中的版本值设置为正确的值。有时,如果您从 session 中分离对象并重新附加它们(合并),则值版本列设置不正确(例如,在 Web 应用程序中,当从表单检索值时)
关于java - Hibernate @Version 字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/810935/