简体   繁体   English

当我使用A JPA Query的getResultList()时,结果是分离还是管理?

[英]When I use A JPA Query's getResultList(), are the results detached or managed?

When I have a JPA Query that I call .getResultList(), It gives me back a List of objects. 当我有一个我调用.getResultList()的JPA查询时,它会返回一个对象列表。 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? 也就是说,如果我对它们进行更改,我是否必须担心合并或持久化它们,还是会自动获取这些更改?

Yes, the objects returned from .getResultList() are managed. 是的,管理从.getResultList()返回的对象。

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. 当您对托管对象进行更改时,您不必担心合并,因为EntityManager自动拾取这些更改。

The managed objects will become detached when the EntityManager that is used to load that object is close() , clear() or detach() . 当用于加载该对象的EntityManagerclose()clear()detach()时,托管对象将分离。 Detached objects are not manged anymore and you should do merging to let the EntityManager pick up the changes. 分离的对象不再被管理,您应该合并以让EntityManager获取更改。

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

From my experience the getResultList() return values are Attached. 根据我的经验,getResultList()返回值是Attached。 That is, you do not have to manually persist them if you make modifications to them within the same transaction. 也就是说,如果在同一事务中对它们进行修改,则不必手动保留它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM