Hibernate查询缓存
来源:互联网 发布:淘宝新品怎么做爆款 编辑:程序博客网 时间:2024/06/06 03:47
一级,二级缓存都是对整个实体进行缓存,它不会缓存普通属性。如果想对普通属性进行缓存,那我们就需要使用查询缓存。但是在多数情况下查询缓存不能提高应用程序的性能,反而降低了应用程序的性能。如果经常使用相同的查询语句,且查询参数也相同,这个时候使用查询缓存才能起到好的效果。
1.配置查询缓存
<property name="hibernate.cache.use_query_cache">true</property>2.实例演示
package cn.codeWang.entity;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Entity(name = “person”)
@Cacheable
//选择缓存策略
@org.hibernate.annotations.Cache(usage =CacheConcurrencyStrategy.READ_ONLY)
public class Person {
@Id
@GeneratedValue
private int id;
private String name;
private String sex;
private String department;
public Person() {}public Person(String name, String sex, String department) { super(); this.name = name; this.sex = sex; this.department = department;}public String getName() { return name;}public String getSex() { return sex;}public String getDepartment() { return department;}
}
<diskStore path="java.io.tmpdir"/><defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /><!-- Sample cache named sampleCache1 This cache contains a maximum in memory of 10000 elements, and will expire an element if it is idle for more than 5 minutes and lives for more than 10 minutes. If there are more than 10000 elements it will overflow to the disk cache, which in this configuration will go to wherever java.io.tmp is defined on your system. On a standard Linux system this will be /tmp" --><cache name="cn.codeWang.entity.Person" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /><!-- Place configuration for your caches following -->
3.同一个session使用两条相同的查询语句
@Test
public void testQuery(){
List<Person> list=session.createQuery("from person").setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } list=session.createQuery("from person").setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } }
控制台输出结果:
Hibernate:
select
person0_.id as id1_0_0_,
person0_.department as departme2_0_0_,
person0_.name as name3_0_0_,
person0_.sex as sex4_0_0_
from
person person0_
where
person0_.id=?
姓名=凯耐
姓名=凯耐
4.不同session使用两条相同的查询语句
@Test
public void testQuery(){
List<Person> list=session.createQuery("from person").setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } //第二次获得session对象 session = sessionFactory.openSession(); list=session.createQuery("from person").setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } }
控制台输出结果:
Hibernate:
select
person0_.id as id1_0_0_,
person0_.department as departme2_0_0_,
person0_.name as name3_0_0_,
person0_.sex as sex4_0_0_
from
person person0_
where
person0_.id=?
姓名=凯耐
姓名=凯耐
5.同一个session使用两条不同的查询语句
@Testpublic void testQuery(){ List<Person> list=session.createQuery("from person").setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } list=session.createQuery("from person where id=:xx").setParameter("xx",2).setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } }
控制台输出结果:
Hibernate:
select
person0_.id as id1_0_0_,
person0_.department as departme2_0_0_,
person0_.name as name3_0_0_,
person0_.sex as sex4_0_0_
from
person person0_
where
person0_.id=?
姓名=凯耐
姓名=凯耐
6.不同session使用两条不同的查询语句
@Test
public void testQuery(){
List<Person> list=session.createQuery("from person").setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } //第二次获得session对象 session = sessionFactory.openSession(); list=session.createQuery("from person where id=:xx").setParameter("xx",2).setCacheable(true).list(); for(Person p:list){ System.out.println(p.getName()); } }
控制台输出结果:
Hibernate:
select
person0_.id as id1_0_0_,
person0_.department as departme2_0_0_,
person0_.name as name3_0_0_,
person0_.sex as sex4_0_0_
from
person person0_
where
person0_.id=?
姓名=凯耐
姓名=凯耐
“`
- Hibernate缓存☞查询缓存
- hibernate缓存之【查询缓存】
- Hibernate查询与缓存
- Hibernate查询缓存
- Hibernate查询缓存综合
- hibernate查询缓存
- hibernate的查询缓存
- hibernate配置缓存查询
- Hibernate查询缓存
- Hibernate查询缓存
- hibernate的查询缓存
- hibernate查询与缓存
- hibernate查询与缓存
- hibernate查询与缓存
- hibernate查询缓存
- hibernate查询与缓存
- Hibernate---->查询缓存
- hibernate配置缓存查询
- JAVA win7下cmd编译Java源代码
- 【二分图匹配入门专题1】L
- tensorflow系列(3)分布式tensorflow
- LINUX的XEN和KVM到底区别在什么地方?
- 斐波那契数列在php中的简单实现
- Hibernate查询缓存
- matlab快速入门2——数据载入与保存
- python django日志器的使用及配置
- beego使用orm插入大量数据,回滚报错:buffer busy
- ArrayList概念及手写代码
- ThreadLocal原理
- tensorflow系列(4)tfrecords的使用
- git merge,rebase和*(no branch)
- java的Map和Map.Entry