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?
3 Answers
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.

- 68
- 1
- 7

- 84,777
- 26
- 143
- 172
-
Thank you so much. I thought they would be attached, but it seemed like they were detached. It turns out that I was mistakenly creating a second EntityManager so it seemed that they were detached. – Philihp Busby Mar 14 '12 at 21:58
-
Do you have to .commit() the changes or is that automatic too? – user2130951 Mar 06 '14 at 16:59
-
I believe it's automatic, however it shouldn't hurt to do so. – Philihp Busby Mar 17 '14 at 14:19
-
Answer from @steliyan-georgiev works for me. If the EntityManager is run without transaction, returned results is not managed. – tan9 Dec 14 '18 at 07:35
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.

- 61
- 2
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.

- 10,995
- 4
- 45
- 75