Hibernate的“未保存值映射不正确"是什么意思? [英] What does mean Hibernate's "unsaved-value mapping was incorrect"?
问题描述
有一个著名的例外:
org.hibernate.StaleObjectStateException:行已由更新或删除 另一笔交易(或未保存的值映射不正确):[my.Entity#123456]
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
Grails 2.2.0
推荐答案
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.
在极少数情况下,当saveOrUpdate()无法确定实体是新实体还是分离实体时,Hibernate需要此功能.
Hibernate needs this in rare cases, when saveOrUpdate() is not able to determine if the entity is new or detached.
如果一个实体是通过您的未保存值确定为分离的,但它是新的,则hibernate无法比较版本号(因为该实体在数据库中不存在).但是Hibernate无法知道您的未保存值映射是否正确或该实体已在另一笔交易中删除. org.hibernate.StaleStateException中也对此进行了描述:
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.
这篇关于Hibernate的“未保存值映射不正确"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!