Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 446
Philihp Busby Avatar asked Feb 05 '12 23:02

Philihp Busby


People also ask

What does getResultList return?

getResultList executes the JPQL SELECT statement and returns the results as a List ; if no results are found then it returns an empty list.

Which query is executed by getResultList ()?

The getResultList() executes the query, just as the docs say. Of course, if this is a TypedQuery , you should add the generics part. It is good practice to return an unmodifiable, empty list from queries without result records.

Which method is used for getting single object as result out of query in JPA?

JPA getSingleResult() or null.

What does getSingleResult return?

NoResultExceptionJPA exceptionThrown by the persistence provider when Query. getSingleResult() or TypedQuery. getSingleResult()is executed on a query and there is no result to return.


1 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.

like image 195
Ken Chan Avatar answered Sep 29 '22 22:09

Ken Chan