3

I understand that we will be able to set hibernate caching for query by using query.setHint("org.hibernate.cacheable", true) like the example below. However, does anyone know of any way to do it for CriteriaQuery instead of Query?

Query query = entityManager.createQuery("from " + Employee.class.getName());
query.setHint("org.hibernate.cacheable", true);
return query.getResultList();
Sebastian
  • 63
  • 7

2 Answers2

2

If you have a CriteriaQuery, you can easily convert it to an "regular" Query.

...
c.select(root).where(p);
Query<MyEntity> regularQuery = this.currentSession().createQuery(c);
regularQuery.setHint("org.hibernate.cacheable", true);
return regularQuery.getResultList();
user3700169
  • 83
  • 1
  • 7
1

For CriteriaQuery you can type cast your CriteriaQuery object to Query from javax.persistence.Query package Like below:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery query = cb.createQuery();

((Query)query).setHint("org.hibernate.cacheable", true);
NickCoder
  • 1,504
  • 2
  • 23
  • 35