openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Gonzalez <adr_gonza...@yahoo.fr>
Subject Re: Portable way to use query cache
Date Wed, 23 Jan 2013 13:21:55 GMT


1. After looking a little bit, I don't think we can use hint for query caching in OpenJPA.
If this is confirmed, I'll open a JIRA for this.


2. Meanwhile, I've created this little utility [1] to do enable portable query caching.

I've tested it only with Hibernate for the moment, but it should work with OpenJPA and toplink.

To use it, just do :

JpaUtils.cacheQuery(entityManager.createNamedQuery("myNamedQuery", Civilite.class)).getResultList();


3. Perhaps there's a better approach - using a decorating entityManager, which decorates the
query and intercepts setHint calls and call the appropriate API whenever the hint is "query.cache"
(or sthing like that) :
Then you'll just have to do :
entityManager.createNamedQuery("myNamedQuery", Civilite.class).setHint("query.cache").getResultList(); 


With a CDI producer - or equivalent doing :

@PersistenceContext
private EntityManager em;

@Produces
public EntityManager getEntityManager () {
  return new EntityManagerDecorator(em);
}

WDYT ?

[1] JpaUtils code :
public final class JpaUtils {
public static <X extends Query> X cacheQuery(X query) {
return cacheQuery(query, true);
}
public static <X extends Query> X cacheQuery(X query, boolean cache) {
String queryClassname = query.getClass().getName(); 
if (queryClassname.startsWith("org.hibernate")) {
query.setHint("org.hibernate.cacheable", cache);
} else if (queryClassname.startsWith("org.apache.openjpa")) {
OpenJPAEntityManager oem = ((OpenJPAQuery<?>) query).getEntityManager();
OpenJPAEntityManagerFactory oemf = oem.getEntityManagerFactory();
QueryResultCache qcache = oemf.getQueryResultCache();
if (cache == true) {
qcache.pin(query);
} else {
qcache.unpin(query);
}
} else if (queryClassname.startsWith("org.eclipse.persistence")) {
query.setHint("eclipselink.query-results-cache", true);
}
return query;
}
}

----- Mail original -----
De : Adrian Gonzalez <adr_gonzalez@yahoo.fr>
À : "users@openjpa.apache.org" <users@openjpa.apache.org>
Cc : 
Envoyé le : Mercredi 23 janvier 2013 10h38
Objet : Portable way to use query cache

Hello,

I'm a newbie with openJPA, I would like to know what's the best way to do query caching (in
a JPA 2.x portable way).
My webapp will run on JBoss 7.x (Hibernate) and Websphere 8 (OpenJPA).


From , [1] there's some proprietary API involved.

Is there a portable way, i.e. using hints instead [2]?

Thanks !

[1] http://openjpa.apache.org/builds/2.0.1/apache-openjpa-2.0.1/docs/manual/main.html
OpenJPAEntityManagerFactory oemf = OpenJPAPersistence.cast(emf);
QueryResultCache qcache = oemf.getQueryResultCache();

[2] entityManager.createNamedQuery(
NamedQueries.ETAT_CONTRAT_FIND_ALL, EtatContrat.class).setHint("openjpa.querycache", true);


Mime
View raw message