entityManager.getTransaction().rollback() 分离实体?

发布于 2024-11-24 12:16:07 字数 574 浏览 1 评论 0原文

我有以下代码:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("test")
EntityManager entityManager = emf.createEntityManager()
User user = entityManager.find(User.class, 0);
entityManager.getTransaction().begin();
entityManager.getTransaction().rollback();
entityManager.refresh(user);

这会在第四行抛出一个 IllegalArgumentException ,表示“实体不受管理”。如果我将第三行更改为 .commit() 而不是 .rollback(),一切似乎都工作正常。

这是怎么回事?我可以阻止这种情况发生吗?

更新: @DataNucleus 正在引导我走向 PersistenceContext。如何更改代码中的持久性上下文?

I have the following piece of code:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("test")
EntityManager entityManager = emf.createEntityManager()
User user = entityManager.find(User.class, 0);
entityManager.getTransaction().begin();
entityManager.getTransaction().rollback();
entityManager.refresh(user);

This throws an IllegalArgumentException on the fourth line saying "Entity not managed". If I change the third line to .commit() instead of .rollback(), everything seems to work fine.

What is going on here? Can I prevent this from happening?

UPDATE: @DataNucleus is directing me towards PersistenceContext. How do I change the persistence context in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-12-01 12:16:07

来自 Eval 2.0 Eval 的 JSR-000317 持久性规范

3.3.2 事务回滚

对于事务范围和扩展持久性上下文,事务回滚会导致所有预先存在的托管实例和已删除的实例
[31]
变得超然
。实例的状态将是
事务回滚时实例的状态。通常事务回滚
导致持久化上下文在回滚时处于不一致的状态。特别是,
版本属性的状态和生成的状态(例如,生成的主键)可能不一致。
以前由持久化上下文管理的实例(包括之前由持久化上下文管理的新实例)
因此,可能无法像其他分离的那样以相同的方式重用
对象 - 例如,它们在传递给合并操作时可能会失败。
[32]

From the JSR-000317 Persistence Specification for Eval 2.0 Eval:

3.3.2 Transaction Rollback

For both transaction-scoped and extended persistence contexts, transaction rollback causes all pre-existing 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]

黑寡妇 2024-12-01 12:16:07

在“Transaction”的 PersistenceContext 中,提交/回滚将分离事务中使用的对象。在“扩展”的 PersistenceContext 中,提交/回滚不会执行类似操作,并且对象会在 EM 关闭时分离。取决于您的具体情况

In a PersistenceContext of "Transaction" then commit/rollback will detach objects used in the transaction. In PersistenceContext of "Extended" then commit/rollback do nothing like that, and objects are detached at close of the EM. Depends on your context

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文