em.getEntityManagerFactory().getCache().evictAll(); operates on the shared second level cache, but the EntityManager itself still has a cache of managed entities. You can simply call em.clear() after em.flush() to detach all managed entities.

em.getEntityManagerFactory().getCache().evictAll(); operates on the shared second level cache, but the EntityManager itself still has a cache of managed entities. You can simply call em.clear() after em.flush() to detach all managed entities.