# Query caching with JPA 2.1

**URL:** <https://discourse.hibernate.org/t/query-caching-with-jpa-2-1/2169>\
**Category:** Hibernate ORM\
**Created:** [January 30, 2019, 3:30am UTC](https://discourse.hibernate.org/t/query-caching-with-jpa-2-1/2169 "2019-01-30T03:30:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![systemasephiro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/systemasephiro/32/95_2.png) [@systemasephiro](https://discourse.hibernate.org/u/systemasephiro)\
**Post date:** [January 30, 2019, 3:30am UTC](https://discourse.hibernate.org/t/query-caching-with-jpa-2-1/2169/1 "2019-01-30T03:30:13Z")

</div>

Hi there,

I’m trying to set up both query caching (and eventually second level caching) purely with JPA 2.1, and not with any hibernate-specific configuration, in a JavaEE 7 environment.

In my persistence.xml `<persistence-unit>` I have:

`<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>`

In my `@Cacheable` entity I have the class annotation:

```auto
@NamedQuery( name = "Thing.findByName", query = "SELECT t FROM Thing t WHERE t.name = :name")

```

And finally in my calling context I have:

```auto
em.createNamedQuery("Thing.findByName", Thing.class)
                .setParameter("name", name)
                .getSingleResult();

```

But each hit on the above query is still querying to the database. Is there anything I’m missing?

Also, is it true that query caching isn’t worthwhile in a clustered environment?

Finally, would using query caching make it so that I can eventually use JPA’s second level cache for lookups that aren’t simply by ID, since the query cache is getting a list of IDs for me?

Thanks!

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 30, 2019, 8:28am UTC](https://discourse.hibernate.org/t/query-caching-with-jpa-2-1/2169/2 "2019-01-30T08:28:47Z")

</div>

There is no way you can use the second-level cache only with JPA-specific annotations because you need to set the [cache concurrency strategy](https://vladmihalcea.com/how-does-hibernate-read_write-cacheconcurrencystrategy-work/).

Check out [this article](https://vladmihalcea.com/how-does-hibernate-query-cache-work/) for more details about using the Hibernate query cache.

---

<div class="post-metadata">

**Author:** ![systemasephiro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/systemasephiro/32/95_2.png) [@systemasephiro](https://discourse.hibernate.org/u/systemasephiro)\
**Post date:** [February 1, 2019, 3:14am UTC](https://discourse.hibernate.org/t/query-caching-with-jpa-2-1/2169/3 "2019-02-01T03:14:12Z")

</div>

That’s too bad.

Should query caching be doable with the details mentioned above?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [February 1, 2019, 8:21am UTC](https://discourse.hibernate.org/t/query-caching-with-jpa-2-1/2169/4 "2019-02-01T08:21:09Z")

</div>

> Should query caching be doable with the details mentioned above?

I’m not sure what do you mean with “details mentioned above”.

Query Cache works only if you explicitly activate it using:

- the `hibernate.cache.use_query_cache` configuration property
- the `org.hibernate.cacheable` JPA query hint on every cacheable query
