Disable caching in JPA (eclipselink)

final Query readQuery = this.entityManager.createQuery(selectQuery);
readQuery.setParameter(paramA, valueA);

// Update the JPA session cache with objects that the query returns.
// Hence the entity objects in the returned collection always updated.
readQuery.setHint(QueryHints.REFRESH, HintValues.TRUE);

entityList = readQuery.getResultList();

This works for me.


This behavior is correct, otherwise if you change object one and object two with different values you will have problems when persisting them. What is happening is the call to load object two updates the entity loaded in the first call. They must point to the same object since they ARE the same object. This ensures that dirty data cannot be written.

If you call em.clear() between the two calls, entity one should become detached your check will return false. There is however no need to do that, eclipse link is infact updating your data to the latest which I would guess is what you want since it frequently changes.

On a side note if you wish to update this data using JPA you will need to be obtaining pessimistic locks on the Entity so that the underlying data cannot change in the DB.

You will need to disable the query cache as well your cache options were just removing the object cache from play not the query cache, that is why you are not getting the new results:

In your code:

em.createNamedQuery("MyLocation.findMyLoc").setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache).getResultList().get(0);

Or in persistence.xml:

<property name="eclipselink.query-results-cache" value="false"/>