tags:

views:

124

answers:

2

Hi everybody, 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

A: 

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.

ambassador86
if i clear the cache so there is no need to call refresh, isn't it?
Navid
my problem is how to refresh a collection of entities
Navid
the refresh overwrites recently changes from db to the object, so you have to call it for every entity object
ambassador86
you mean I must call refresh in for loop on the collection??!!!!
Navid
what happen if the collection has 10000 record ?!!!!
Navid
if you clear the cache before you call getResultList() you don't need to use em.refresh()
ambassador86
Thanx,em.getEntityManagerFactory().getCache().evictAll(); works for me.
Navid
If you want you can mark this question answered.
ambassador86
+1  A: 

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.

Mike
Not Work!-> setHint("javax.persistence.cache.retrieveMode", "BYPASS");i'm using JPA2 eclipselink and glassfish v3 with all default setting and coding
Navid
if i call em.getEntityManagerFactory().getCache().evictAll(); befor quering it works But I dont want to clear all L2 cache for this query.how to turn your answer to work properly?
Navid
eclipselink doesnt have refreshAll !!
Navid
Sorry didn't mean to imply that all JPA providers have a refreshAll - just that your vendor might have something similar. I'll edit to make it clearer.
Mike
Since you're using EclipseLink you could try their hints instead of the property from the spec. Here are a couple of examples:http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/cache_usaged#Used_as_NamedQuery_propertieshttp://www.eclipse.org/eclipselink/api/2.0/org/eclipse/persistence/config/QueryHints.html
Mike