Tutorial :Refreshing collection (getResultList) of entities by entityManager.refresh



Question:

How to refresh getResultList collection of entities when JPA cache is enabled. I mean:

List customers = query.getResultList();

????? > em.refresh ( customers ) ! // i need refresh because the cache is enabled.

RGDS Navid


Solution:1

In JPA 2.0 it might be easier to skip the L2 cache entirely, by using a query hint. For example :

Query query = em.createQuery(...);   query.setHint("javax.persistence.cache.retrieveMode", "BYPASS"); // skip the L2 cache.  List customers = query.getResultList();  

This isn't available in JPA 1.0 though. If you're on JPA 1.0 you may have to use a vendor specific API. I believe Hibernate provides something similar to the JPA 2.0 hint (other providers might also have this mechanism). OpenJPA has a refreshAll(Collection c) method that should also work for you, and I suspect other providers have something similar. EclipseLink doesn't seem to have one though.


Solution:2

Before you call em.refresh() you should clear the cache with em.getEntityManagerFactory().getCache().evictAll();

This is a new feature in JPA2, so you must update your ORM framework probably.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »