当我使用 JPA 查询的 getResultList() 时,结果是分离的还是托管的?

发布于 2025-01-02 22:07:23 字数 129 浏览 1 评论 0原文

当我有一个名为 .getResultList() 的 JPA 查询时,它会返回一个对象列表。该列表中的对象是托管的还是分离的?也就是说,如果我对它们进行更改,我是否需要担心稍后合并或保留它们,或者这些更改会自动生效吗?

When I have a JPA Query that I call .getResultList(), It gives me back a List of objects. Are the objects in that list managed or detached? That is, do I have to worry about merging or persisting them later if I make changes to them, or will those changes be picked up automatically?

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

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

发布评论

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

评论(3

请恋爱 2025-01-09 22:07:23

是的,从 .getResultList() 返回的对象是受管理的。

当您对托管对象进行更改时,您不必担心合并,因为 EntityManager 将自动获取这些更改。

当用于加载该对象的 EntityManager 为 关闭()clear()分离()。分离的对象不再被管理,您应该进行合并以使 EntityManager 拾取更改。

Yes, the objects returned from .getResultList() are managed.

When you made changes on the managed objects, you do not worry about merging as those changes will be picked up by the EntityManager automatically.

The managed objects will become detached when the EntityManager that is used to load that object is close(), clear() or detach(). Detached objects are not manged anymore and you should do merging to let the EntityManager pick up the changes.

〃安静 2025-01-09 22:07:23

如果您当前处于事务中,则它们将受到管理,但如果您不在事务中(例如,如果您使用 TransactionAttributeType.NOT_SUPPORTED 或 TransactionAttributeType.NEVER 注释了事务),则您的实体将不会受到管理。

They will be managed if you are currently within a transaction, but if you are not (e.g. if you have annotated your trnasction with TransactionAttributeType.NOT_SUPPORTED or TransactionAttributeType.NEVER) your entities will not be managed.

筱武穆 2025-01-09 22:07:23

根据我的经验,getResultList() 返回值已附在后面。也就是说,如果您在同一事务中对它们进行修改,则不必手动保留它们。

From my experience the getResultList() return values are Attached. That is, you do not have to manually persist them if you make modifications to them within the same transaction.

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