回滚后,JPA和Hibernate实体标识符是否重置为null? [英] Are JPA and Hibernate entity identifiers reset to null after a rollback?

查看:59
本文介绍了回滚后,JPA和Hibernate实体标识符是否重置为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我回滚,会话中的实体会怎样?他们会在交易之前回到状态吗?特别是他们会获得新的ID吗?

What happens with Entities in session if I make rollback ? Do they get back to the state before transaction ? In particular do they get new ids ?

示例:

session.startTransaction();
Entity e = new Entity(); //e.id == null
session.save (e);        //suppose it was ok
session.rollback();      // e.id == ???

更新:

我已经进行了Hibernate 4测试.测试后,该实体已成为新的ID.

I've made the Hibernate 4 test. After the test the entity has become a new id.

推荐答案

我将简单引用JPA实现(3.3.2事务回滚):

I will simply quote from the JPA implementation (3.3.2 Transaction Rollback):

对于事务作用域和扩展的持久性上下文,事务回滚会导致所有先前存在的托管实例和删除了实例[31]以使其脱离.实例的状态为事务处理时实例的状态回滚.事务回滚通常会导致持久性上下文在回滚时处于不一致状态.在特别是版本属性的状态和生成的状态(例如,生成的主键)可能不一致.当时的实例以前由持久性上下文(包括新实例)管理因此在该交易中被永久保留)以与其他分离对象相同的方式可重复使用,例如,它们传递给合并操作时可能会失败.[32]

For both transaction-scoped and extended persistence contexts, transaction rollback causes all pre-exist-ing managed instances and removed instances[31] to become detached. The instances’ state will be the state of the instances at the point at which the transaction was rolled back. Transaction rollback typically causes the persistence context to be in an inconsistent state at the point of rollback. In particular, the state of version attributes and generated state (e.g., generated primary keys) may be inconsistent. Instances that were formerly managed by the persistence context (including new instances that were made persistent in that transaction) may therefore not be reusable in the same manner as other detached objects—for example, they may fail when passed to the merge operation.[32]

这实际上意味着,这取决于在调用 em.rollback()之前是否具有ID(取决于FlushMode和JPA的实现).如果分配了ID,则该ID将保持设置状态.如果没有,那么您将没有ID.

This actually means, that it depends whether you have or not an ID right before calling em.rollback() (which depends on FlushMode & JPA implementation). If an ID was assigned, than the ID will remain set. If not, then you will have no ID.

这篇关于回滚后,JPA和Hibernate实体标识符是否重置为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆