Hibernate does not use the first- and second-level for queries. Is there any way to cache the result of a query?
Don’t want to read? You can watch it here!
Hibernate also supports the QueryCache, which can store the result of a query. You need to activate it in the persistence.xml file by setting the parameter hibernate.cache.use_query_cache to true and defining a hibernate.cache.region.factory_class (see l. 12-13).
And you also need to activate caching for the specific query of which you want to cache the results by calling the setCacheable of the Hibernate-specific Query interface with the parameter true.
Get this Hibernate Tip as a printable PDF!Join the free Thoughts on Java Library to get access to lots of member-only content, like a printable PDF for this post, lots of cheat sheets and 2 ebooks about Hibernate.
I get into more details about Hibernate’s 3 different kinds of caches in the Hibernate Performance Tuning Online Training.
Hibernate Tips Book
It gives you more than 70 ready-to-use recipes for topics like basic and advanced mappings, logging, Java 8 support, caching and statically and dynamically defined queries.
Get it now as a paperback, ebook or PDF.