hibernate查询缓存
查询缓存是针对普通属性结果集的缓存——个人推荐用这个,单独用查询缓存。
对实体对象的结果集只缓存id ,这配合hibernate二级缓存一起用.
查询缓存的生命周期,当前关联的表发生修改,那么查询缓存生命周期结束——推荐用来,查询那些不经常变更的数据,如数据字典。
查询缓存只对query.list()起作用,query.iterate不起作用,也就是query.iterate不使用
查询缓存的配置和使用:
* 在hibernate.cfg.xml文件中启用查询缓存,如:
<property name="hibernate.cache.use_query_cache">true</property> //默认是fasle
* 在程序中必须手动启用查询缓存,如:
query.setCacheable(true);
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://192.168.1.3:3306/hibernate
</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<!-- 开启二级缓存 ,默认开启 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定缓存产品提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 开启查询缓存,默认不开启false -->
<property name="hibernate.cache.use_query_cache">true</property>
<mapping resource="com/model/Classes.hbm.xml" />
<mapping resource="com/model/Student.hbm.xml" />
<!-- 放在mpping后,指定哪些类为二级缓存-->
<class-cache class="com.model.Student" usage="read-only"/>
</session-factory>
</hibernate-configuration>
普通属性结果集测试用例:
public void testList3() {
Session session = null;
Transaction ta = null;
try {
session = HibernateUtil.getSession();
ta = session.beginTransaction();
Query query = session.createQuery("select name from Student");
//开启查询缓存
query.setCacheable(true);
List stuList = query.list(); //发出sql
for(Iterator it = stuList.iterator(); it.hasNext();){
String stuName = (String)it.next();
System.out.println(stuName);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
if (ta != null) {
ta.rollback();
}
} finally {
// 关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
System.out.println("========第二次查询============");
try {
session = HibernateUtil.getSession();
ta = session.beginTransaction();
Query query = session.createQuery("select name from Student");
//开启查询缓存
query.setCacheable(true);
List stuList = query.list(); // 不发出sql,查询缓存与session的生命周期无关
for(Iterator it = stuList.iterator(); it.hasNext();){
String stuName = (String)it.next();
System.out.println(stuName);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
if (ta != null) {
ta.rollback();
}
} finally {
// 关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
}
======================================
执行以下实体对象测试用例
public void testList() {
Session session = null;
Transaction ta = null;
try {
session = HibernateUtil.getSession();
ta = session.beginTransaction();
Query query= session.createQuery("from Student");
query.setCacheable(true);//开启查询缓存
List stuList = query.list();
for(Iterator it = stuList.iterator(); it.hasNext();){
Student stu = (Student)it.next();
System.out.println(stu.getName());
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
if (ta != null) {
ta.rollback();
}
} finally {
// 关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
System.out.println("=============================");
try {
session = HibernateUtil.getSession();
ta = session.beginTransaction();
Query query= session.createQuery("from Student");
query.setCacheable(true);//开启查询缓存
List stuList = query.list();
for(Iterator it = stuList.iterator(); it.hasNext();){
Student stu = (Student)it.next();
System.out.println(stu.getName());
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
if (ta != null) {
ta.rollback();
}
} finally {
// 关闭session, user变为detached离线对象
HibernateUtil.closeSession(session);
}
}
1)hibernate 查询缓存true 与 二级缓存fasle
<!-- 开启二级缓存 ,默认开启 -->
<property name="hibernate.cache.use_second_level_cache">false</property>
<!-- 指定缓存产品提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 开启查询缓存,默认不开启false -->
<property name="hibernate.cache.use_query_cache">true</property>
<mapping resource="com/model/Classes.hbm.xml" />
<mapping resource="com/model/Student.hbm.xml" />
<!-- 放在mpping后,指定哪些类为二级缓存-->
<class-cache class="com.zd.model.Student" usage="read-only"/>
输出结果:
13:59:43,589 WARN EhCacheProvider:86 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
13:59:43,620 WARN EhCacheProvider:86 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.class_id as class3_1_ from student_join student0_
学生1
学生2
学生3
学生4
学生5
学生6
学生7
学生8
=============================
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
Hibernate: select student0_.id as id1_0_, student0_.name as name1_0_, student0_.class_id as class3_1_0_ from student_join student0_ where student0_.id=?
学生1
学生2
学生3
学生4
学生5
学生6
学生7
学生8
分析:
//会发出n条查询语句,因为开启了查询缓存,关闭了二级缓存,那么查询缓存会缓存实体对象的id
//所以hibernate会根据实体对象的id去查询相应的实体,如果缓存中不存在相应的
//实体那么将发出根据实体id查询的sql语句,否则不会发出sql使用缓存中的数据
2)hibernate 查询缓存true 与 二级缓存true
<!-- 开启二级缓存 ,默认开启 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定缓存产品提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 开启查询缓存,默认不开启false -->
<property name="hibernate.cache.use_query_cache">true</property>
<mapping resource="com/model/Classes.hbm.xml" />
<mapping resource="com/model/Student.hbm.xml" />
<!-- 放在mpping后,指定哪些类为二级缓存-->
<class-cache class="com.zd.model.Student" usage="read-only"/>
输出结果:
13:59:43,589 WARN EhCacheProvider:86 - Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.
13:59:43,620 WARN EhCacheProvider:86 - Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.
Hibernate: select student0_.id as id1_, student0_.name as name1_, student0_.class_id as class3_1_ from student_join student0_
学生1
学生2
学生3
学生4
学生5
学生6
学生7
学生8
=============================
学生1
学生2
学生3
学生4
学生5
学生6
学生7
学生8
分析:
/不会发出查询sql,因为开启了二级缓存和查询缓存,查询缓存缓存了实体对象的id列表
//hibernate会根据实体对象的id列表到二级缓存中取得相应的数据