gpt4 book ai didi

java - JPA 和 Hibernate 实体标识符是否在回滚后重置为 null?

转载 作者:行者123 更新时间:2023-12-01 10:50:55 25 4
gpt4 key购买 nike

如果我进行回滚, session 中的实体会发生什么情况?他们会在交易前回到状态吗?特别是他们有新的身份证吗?

例子:

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

更新 :

我已经进行了 Hibernate 4 测试。测试后实体已成为新的 id。

最佳答案

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

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。

关于java - JPA 和 Hibernate 实体标识符是否在回滚后重置为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com