hibernate查询缓存(hibernate默认是关闭的)
查询缓存是针对普通属性结果集的缓存
对实体对象的结果集只缓存id
查询缓存的生命周期,当前关联的表发生修改,那么查询缓存生命周期结束
查询缓存的配置和使用:
1. 启用查询缓存:在hibernate.cfg.xml中加入:
<property name=”hibernate.cache.use_query_cache”>true</property>
2. 在程序中必须手动启用查询缓存,如:query.setCacheable(true);
测试查询缓存:
一. 开启查询缓存,关闭二级缓存,开启一个session,分别调用query.list (查询属性)
Query query = session.createQuery(“select s.name from Student s”);
//启用查询缓存
query.setCacheable(true);
List names = query.list();
for(Iterator iter = names.terator();iter.hasNext();){
String name = (String)iter.next();
System.out.println(name);
}
System.out.println(“------------------------------------------”);
query = session.createQuery(“select s.name from Student s”);
//启用查询缓存
query.setCacheable(true);
names = query.list();
for(Iterator iter = names.terator();iter.hasNext();){
String name = (String)iter.next();
System.out.println(name);
}
第二次没有去查询数据库,因为启用了查询缓存
二. 开启查询缓存,关闭二级缓存,开启两个session,分别调用query.list (查询属性)
Query query = session.createQuery(“select s.name from Student s”);
//启用查询缓存
query.setCacheable(true);
List names = query.list();
for(Iterator iter = names.terator();iter.hasNext();){
String name = (String)iter.next();
System.out.println(name);
}
session.close();
System.out.println(“------------------------------------------”);
………
Query query = session.createQuery(“select s.name from Student s”);
//启用查询缓存
query.setCacheable(true);
List names = query.list();
for(Iterator iter = names.terator();iter.hasNext();){
String name = (String)iter.next();
System.out.println(name);
}
第二次没有去查询数据库,因为查询缓存生命周期与session生命周期无关
三. 开启查询缓存,关闭二级缓存,开启两个session,分别调用query.iterate (查询属性)
Query query = session.createQuery(“select s.name from Student s”);
//启用查询缓存
query.setCacheable(true);
for(Iterator iter =query.iterate();iter.hasNext();){
String name = (String)iter.next();
System.out.println(name);
}
session.close();
System.out.println(“------------------------------------------”);
………
Query query = session.createQuery(“select s.name from Student s”);
//启用查询缓存
query.setCacheable(true);
for(Iterator iter = query.iterate();iter.hasNext();){
String name = (String)iter.next();
System.out.println(name);
}
第二去查询数据库,因为查询缓存只对query.list()起作用,对query.iterate()不起作用,也就是说query.iterate()不使用查询缓存
四. 关闭查询缓存,关闭二级缓存,开启两个session,分别调用query.list (查询实体对象)
Query query = session.createQuery(“ from Student s”);
//query.setCacheable(true);
List students = query.list();
for(Iterator iter = students.iterate();iter.hasNext();){
Student stu = (Student)iter.next();
System.out.println(stu.getName());
}
session.close();
System.out.println(“------------------------------------------”);
………
Query query = session.createQuery(“ from Student s”);
//query.setCacheable(true);
List students = query.list();
for(Iterator iter = students.iterate();iter.hasNext();){
Student stu = (Student)iter.next();
System.out.println(stu.getName());
}
第二去查询数据库,因为list默认每次都会发出查询sql
五. 开启查询缓存,关闭二级缓存,开启两个session,分别调用query.list (查询实体对象)
Query query = session.createQuery(“ from Student s”);
query.setCacheable(true);
List students = query.list();
for(Iterator iter = students.iterate();iter.hasNext();){
Student stu = (Student)iter.next();
System.out.println(stu.getName());
}
session.close();
System.out.println(“------------------------------------------”);
………
Query query = session.createQuery(“ from Student s”);
query.setCacheable(true);
List students = query.list();
for(Iterator iter = students.iterate();iter.hasNext();){
Student stu = (Student)iter.next();
System.out.println(stu.getName());
}
第二去查询数据库时,会发出N条sql语句,因为开启了查询缓存,关闭了二级缓存,那么查询缓存会缓存实体对象的id,所以hibernate会根据实体对象的id去查询相应的实体,如果缓存中不存在相应的实体,那么将发出根据实体id查询的sql语句,否则不会发出sql,使用缓存中的数据
六. 开启查询缓存,开启二级缓存,开启两个session,分别调用query.list (查询实体对象)
Query query = session.createQuery(“ from Student s”);
query.setCacheable(true);
List students = query.list();
for(Iterator iter = students.iterate();iter.hasNext();){
Student stu = (Student)iter.next();
System.out.println(stu.getName());
}
session.close();
System.out.println(“------------------------------------------”);
………
Query query = session.createQuery(“ from Student s”);
query.setCacheable(true);
List students = query.list();
for(Iterator iter = students.iterate();iter.hasNext();){
Student stu = (Student)iter.next();
System.out.println(stu.getName());
}
第二不会发出sql,因为开启了二级缓存和查询缓存,查询缓存缓存了实体对象的id列表,hibernate会根据实体对象的id列表到二级缓存中取得相应的数据