openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adrian Gonzalez <>
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 :

private EntityManager em;

public EntityManager getEntityManager () {
  return new EntityManagerDecorator(em);


[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) {;
} else {
} else if (queryClassname.startsWith("org.eclipse.persistence")) {
query.setHint("eclipselink.query-results-cache", true);
return query;

----- Mail original -----
De : Adrian Gonzalez <>
À : "" <>
Cc : 
Envoyé le : Mercredi 23 janvier 2013 10h38
Objet : Portable way to use query cache


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 !

OpenJPAEntityManagerFactory oemf = OpenJPAPersistence.cast(emf);
QueryResultCache qcache = oemf.getQueryResultCache();

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

View raw message