O'Reilly logo

Hands-On High Performance with Spring 5 by Dinesh Radadiya, Prashant Goswami, Pritesh Shah, Subhash Shah, Chintan Mehta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Query cache

The query cache can be enabled by adding the following Hibernate property:

hibernate.cache.use_query_cache=true

Once the query cache is enabled, we can specify which query we want to cache, as follows:

Query query = entityManager.createQuery("SELECT a FROM Account a WHERE a.accountId=:accountId", Account.class);query.setParameter("accountId", 7L);query.setHint(QueryHints.HINT_CACHEABLE, true);Account account = (Account)query.getSingleResult();

If we execute the same query again which is cached by query cache, the following is the log printed with DEBUG mode:

2018-03-17 15:39:07 DEBUG StandardQueryCache:181 - Returning cached query results2018-03-17 15:39:07 DEBUG SQL:92 - select account0_.account_id as account_1_0_0_, account0_.name ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required