June 2018
Intermediate to advanced
408 pages
11h 23m
English
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 ...
Read now
Unlock full access