I got a list of Person entities, e.g.
Doe, John
Miller, Pete
Sixpack, John
They are stored in a database and represented in a java.util.List(Person).
Ok, now I edit "Sixpack, John" to "Fivepack, John". The List gets updated like this:
persons = em.createQuery("from Person order by Name").getResultList();
Now there are two scenarios, depending on how the EntityManager got injected:
1. @PersistenceContext
private EntityManager em;
The list now is:
Doe, John
Fivepack, John
Miller, Pete
This is correct.
2. @PersistenceContext(type=EXTENDED)
private EntityManager em;
The list now is:
Doe, John
Sixpack, John
Miller, Pete
The list has the right order (due to the database query) BUT the editied Person still is Sixpack, instead of Fivepack. Why? Cause in the extended PersistenceContext the entity remains managed and so its properties don't get updated after hitting the database.
If I would want to have a list with updated names I'd have to call
//after getResultList()
for(Person p: persons)
em.refresh(p);
but that's idiotic since it causes an additional SELECT query for every Person entity.
QUESTION: Is there any way to tell the EntityManager that he should check if managed entities have changed when retrieving a list of them by a query?