Nav
Nav

Reputation: 4540

Refreshing collection (getResultList) of entities by entityManager.refresh

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

Upvotes: 0

Views: 6317

Answers (2)

Mike
Mike

Reputation: 18974

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.

Upvotes: 5

ambassador86
ambassador86

Reputation: 161

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.

Upvotes: 3

Related Questions