Hibernate query cache does not cache the state of actual entities in the result set, and it caches only identifier values and results of value type. Query cache integrates closely with the second-level cache, and is useful for queries that are executed frequently with the same parameters.
Hibernate configuration file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Settings for a remote MySQL database. --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/mytecbar?autoReconnect=true</property> <property name="connection.username">root</property> <property name="connection.password">45Tn$8ma</property> <property name="connection.autocommit">true</property> <property name="c3p0.min_size">3</property> <property name="c3p0.max_size">6</property> <property name="c3p0.timeout">1000</property> <property name="c3p0.idle_test_period">2000</property> <property name="c3p0.preferredTestQuery">select 1;</property> <!-- Use EHCache for cache--> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <property name="cache.provider_configuration_file_resource_path">/ehcache.xml</property> <property name="cache.use_query_cache">true</property> <property name="cache.use_minimal_puts">false</property> <property name="max_fetch_depth">3</property> <!-- Print SQL to stdout. --> <property name="show_sql">true</property> <property name="use_sql_comments">true</property> <!-- Mapping files --> <mapping resource="net/tecbar/domain/category.hbm.xml" /> </session-factory> </hibernate-configuration> |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package net.tecbar.hibernate; import net.tecbar.domain.Category; import org.hibernate.Query; public class HibernateQueryCacheExample { // private static Logger LOG = Logger.getLogger(HibernateSecondLevelCacheExample.class); // we enabled 2nd level cache public static void main(String[] args) throws InterruptedException { // use query cache Category category = obtainCategory(3); // The entity was removed after the previous session close. HibernateUtil.closeSession(); System.out.println(category); Category category2 = obtainCategory(3); HibernateUtil.closeSession(); System.out.println(category2); } private static Category obtainCategory(int cid) { Query query = HibernateUtil.getSession().createQuery("from Category where id = :cid") .setInteger("cid", cid) // can be commented out, or be configured in ehcache.xml .setCacheRegion("my_category_regiron") .setCacheable(true); return (Category)query.setMaxResults(1).uniqueResult(); } } |
Let’s see output:
1 2 3 4 5 |
Hibernate: /* from Category where id = :cid */ select category0_.id as id1_0_, category0_.name as name2_0_, category0_.description as descript3_0_ from category category0_ where category0_.id=? limit ? PIAccount [id=3, name=car, description=2015 models] PIAccount [id=3, name=car, description=2015 models] |
Pingback: Hibernate Caching