EntityManager refresh

entityManager.getEntityManagerFactory().getCache().evictAll()

Refresh is something different since it modifies your object. This line will just empty the cache, so if you fetch objects changed outside the entity manager, it will do an actual database query instead of using the outdated cached value.


If you are using EclipseLink instead of Hibernate the hint is:

em.createNamedQuery("SomeEntity.SomeNamedQuery")
.setHint(QueryHints.REFRESH, true)
.getResultList();

I had a similar issue and the evictAll() line above worked for me.

Alternatively, the @Cache annotation on the entity class worked too, with the benefit of being able to control caching parameters:

@Cache(coordinationType=CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS)

See: http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching