What does mean Hibernate's “unsaved-value mapping was incorrect”?

狂风中的少年 提交于 2020-07-06 07:47:50

问题


There is a famous exception:

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [my.Entity#123456]

This is a very familiar situation when "row was updated or deleted by another transaction".

But what means another possibility - "unsaved-value mapping was incorrect"? And how to reproduce such situation intentionally?

Grails 2.2.0


回答1:


For a description of unsaved-value see http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-id:

unsaved-value (optional - defaults to a "sensible" value): an identifier property value that indicates an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session.

Hibernate needs this in rare cases, when saveOrUpdate() is not able to determine if the entity is new or detached.

If an entity is determined by means of your unsaved-value as detached, but is instead new, then hibernate can't compare the version numbers (because the entity just doesn't exist in the database). But Hibernate can't know if your unsaved-value mapping is not correct or the entity has been deleted in another transaction. This is described in org.hibernate.StaleStateException as well:

Thrown when a version number or timestamp check failed, indicating that the Session contained stale data (when using long transactions with versioning). Also occurs if we try delete or update a row that does not exist.



来源:https://stackoverflow.com/questions/24827576/what-does-mean-hibernates-unsaved-value-mapping-was-incorrect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!