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

Philihp Busby picture Philihp Busby · Feb 6, 2012 · Viewed 7.6k times · Source

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?

Answer

Ken Chan picture Ken Chan · Feb 6, 2012

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.