hibernate二级和查询缓存使用

[size=x-small][b]二级缓存[/b][/size]

一。先导入解压后hibernate-release-4.2.8.Final\lib\optional\ehcache目录下的jar包
ehcache-core-2.4.3.jar
hibernate-ehcache-4.2.8.Final.jar
slf4j-api-1.6.1.jar

二。配置hibernate.cfg.xml

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>



三。拷贝下载包\hibernate-release-4.2.8.Final\project\hibernate-ehcache\bin项目目录下的ehcache.xml文件至项目根目录下

四。在实体类上加上@Cache注释

@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Student{
private int id;
private String name;
private Set<Teacher> students =new HashSet<Teacher>();
@Id
@GeneratedValue
public int getId() {
return id;
}
@ManyToMany(mappedBy="students")
public Set<Teacher> getStudents() {
return students;
}

public void setStudents(Set<Teacher> students) {
this.students = students;
}

public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}



五,测试

public class OneToOneORMappingTest {
public static void main(String[] args) {
Configuration cfg = new AnnotationConfiguration().configure("/hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Student student =(Student) session.load(Student.class, 0);

System.out.println(student.getName());
session.getTransaction().commit();
session.close();

Session session2 = sf.openSession();
session2.beginTransaction();
//查询
Student student2 =(Student) session2.load(Student.class,0);
System.out.println(student2.getName());
session2.getTransaction().commit();
session2.close();
sf.close();

}

}



测试结果,只发出一条语句,输出两次结果

Hibernate:
select
student0_.id as id1_0_0_,
student0_.name as name2_0_0_
from
Student student0_
where
student0_.id=?
zhangsan
zhangsan



[size=x-small][b]查询缓存[/b][/size]
依赖二级缓存
一。配置hibernate.cfg.xml,打开查询缓存

<property name="hibernate.cache.use_query_cache">true</property>


二。测试,查询时注意加setCacheable(true)

public class OneToOneORMappingTest {
public static void main(String[] args) {
Configuration cfg = new AnnotationConfiguration().configure("/hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
//查询
List<Student> result = session.createQuery("from Student").setCacheable(true).list();
for(Student s :result){
System.out.println(s.getName());
}
session.getTransaction().commit();
session.close();

Session session2 = sf.openSession();
session2.beginTransaction();
//查询
List result2 = session2.createQuery("from Student").setCacheable(true).list();

session2.getTransaction().commit();
session2.close();
sf.close();

}
}



测试结果:只发出一条sql语句
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值