BigMemory Go 4.3.4 | Product Documentation | BigMemory Go Integrations | Using BigMemory Go with Hibernate | Configuring ehcache.xml Settings | Ehcache Settings for Queries
Ehcache Settings for Queries
Hibernate allows the caching of query results.
This cache is used if you use a query cache without setting a name. A typical ehcache.xml configuration is:
<persistence strategy="localTempSwap"/>
Tracks the timestamps of the most recent updates to particular tables. It is important that the cache timeout of the underlying cache implementation is set to a higher value than the timeouts of any of the query caches. Therefore, it is recommend that the underlying cache not be configured for expiry at all. A typical ehcache.xml configuration is:
<persistence strategy="localTempSwap"/>
Named Query Caches
In addition, a QueryCache can be given a specific name in Hibernate using Query.setCacheRegion(String name). The name of the cache in ehcache.xml is then the name given in that method. The name can be whatever you want, but by convention you should use "query." followed by a descriptive name. For example:
<cache name="query.AdministrativeAreasPerCountry"
<persistence strategy="localTempSwap"/>
Using Query Caches
Suppose you have a common query running against the Country Domain. Here is the code to use a query cache with it:
public List getStreetTypes(final Country country) throws HibernateException {
final Session session = createSession();
try {
final Query query = session.createQuery(
+ " from StreetType st "
+ " where = :countryId "
+ " order by st.sortOrder desc,");
query.setLong("countryId", country.getId().longValue());
return query.list();
} finally {
The query.setCacheable(true) line caches the query. The query.setCacheRegion("query.StreetTypes") line sets the name of the Query Cache. Alex Miller has a good article on the query cache at
Hibernate CacheConcurrencyStrategy for Queries
None of the read-write, nonstrict-read-write and read-only policies apply to Domain Objects. Cache policies are not configurable for query cache. They act like a non-locking read only cache.

Copyright © 2010 - 2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
Innovation Release