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

落花浮王杯 提交于 2019-12-06 19:35:00

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/9153995/when-i-use-a-jpa-querys-getresultlist-are-the-results-detached-or-managed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!