Hibernate一级缓存
2019独角兽企业重金招聘Python工程师标准>>>
import java.io.Serializable;
import org.hibernate.Session;
import junit.framework.TestCase;
public class CacheLevel1Test extends TestCase {
/**
* 在同一个session中发出两次load查询
*/
public void testCache1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
//不会发出sql,因为load使用缓存
student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次get查询
*/
public void testCache2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.get(Student.class, 1);
System.out.println("student.name=" + student.getName());
//不会发出sql,因为get使用缓存
student = (Student)session.get(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次iterate查询实体对象
*/
public void testCache3() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + student.getName());
//会发出查询id的sql,不会发出查询实体对象的sql,因为iterate使用缓存
student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中发出两次iterate查询实体对象
*/
public void testCache4() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//查询属性而不是实体,故不会在一级缓存中写入数据
String name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + name);
//iterate查询普通属性,一级缓存不会缓存,所以发出sql
//一级缓存是缓存实体对象的
name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();
System.out.println("student.name=" + name);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 开启两个session中发出load查询
*/
public void testCache5() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//会发出查询语句,session间不能共享一级缓存的数据
//因为它会伴随session的生命周期存在和消亡
Student student = (Student)session.load(Student.class, 1);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 在同一个session中先save,在发出load查询save过的数据
*/
public void testCache6() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student stu = new Student();
stu.setName("王五");
Serializable id = session.save(stu);
//不会发出sql,因为save是使用缓存的
Student student = (Student)session.load(Student.class, id);
System.out.println("student.name=" + student.getName());
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 向数据库中批量加入1000条数据
*/
public void testCache7() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
for (int i=0; i<1000; i++) {
Student student = new Student();
student.setName("s_" + i);
session.save(student);
//每20条数据就强制session将数据持久化
//同时清除缓存,避免大量数据造成内存溢出
if ( i % 20 == 0) {
session.flush();
session.clear();
}
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
转载于:https://my.oschina.net/u/2317688/blog/508884
Hibernate一级缓存相关推荐
- hibernate一级缓存_Hibernate缓存–一级缓存
hibernate一级缓存 Welcome to Hibernate Caching – First Level Cache Example Tutorial. Recently we looked ...
- hibernate一级缓存和二级缓存的区别[转]
缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...
- Hibernate 一级缓存,二级缓存,查询缓存
概念: 1.什么是缓存呢? 缓存:是计算机领域的概念,它介于应用程序和永久性数据存储源之间. 缓存:一般人的理解是在内存中的一块空间,可以将二级缓存配置到硬盘.用白话来说,就是一个存储数据的容器.我们 ...
- Hibernate一级缓存与二级缓存的区别
一级缓存: 就是Session级别的缓存.一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中. 如果短时间内这个session(一定要同一个session)又做了同一个操作,那么h ...
- java中一级缓存二级缓存_[Java] hibernate 一级缓存和二级缓存
缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...
- [转] Hibernate一级缓存、二级缓存
缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...
- Hibernate一级缓存、二级缓存以及查询缓存的关系
转载自http://blog.csdn.net/maoyeqiu/article/details/50209893 前两天总结了一下二级缓存和查询缓存的关系,但是又有一个新的问题,就是查询缓存缓存到二 ...
- (五)Hibernate一级缓存
一.简介 缓存,介于应用程序和永久数据存储源之间,作用是为了降低应用程序对物理数据源访问的频率,从而提高应用的运行性能. 例如我们cpu执行效率每秒处理的数据高达上千兆,而我们的硬盘读取速度却没那么 ...
- Hibernate一级缓存常用API
一级缓存特点: 当我们通过session的save,update saveOrupdate进行操作时,如果一级缓存中没有对象,会将这些对象从数据库中查询到,存储到一级缓存. 当我们通过session的 ...
最新文章
- Typescript04---模块、命名空间
- 如何结合IbatisNet的LIST遍历实现模糊查询
- mycat读写分离部署步骤
- cuda笔记-一个Block多线程求卷积
- 读EasyPR开发详解实践感想1
- 移植 Qt4.8.5到Tiny210
- String类的编码和解码问题
- php该怎么下载文件,php怎么实现下载文件?
- 西门子S7系列中间人攻击:防御和流量异常检测(三)
- 中国联通骨干网网络介绍
- python setup.py install没反应_花了两天,终于把 Python 的 setup.py 给整明白了
- 在家如何下载nature中的文献
- 深度学习在视频行为识别中应用
- 平安性格测试题及答案_【中国平安人寿保险平安保险性格测试特别是智商题通过率怎么样】-看准网...
- 天翼云云硬盘的磁盘模式及共享盘
- #8 实现指定函数swap(结构体)
- 外文翻译原文附在后面_外文翻译原文及配套译文
- 使用circlize包绘制和弦图
- 墨菲定律、二八法则、马太效应、彼得原理、酒与污水定律、水桶定律、蘑菇管理原理等13条是左右人生的金科玉律。...
- 北航操作系统课程-20200305课堂小测-启动与系统引导程序装载
热门文章
- java.io.IOException: Incompatible clusterIDs
- linux不能挂载手机,这个无法挂载的信息是怎么回事?
- java 打开服务器excel文件_Java中上传excel文件并在服务器端读取存入数据库
- php7.0 连接ftp,PHPstorm连接ftp
- html 生成唯一码,生成唯一邀请码.html
- python做bi系统_如何使用Python创建可视化对象
- cstring 比较_属牛人和属蛇人姻缘婚配关系比较和谐
- UI自动化测试中的页面定位问题,年薪50W软件测试工程师为你解答
- mysql 5.5 declare_MySQL5新语句declare的用法
- transformer预测过程_2019最新进展 | Transformer在深度推荐系统中的应用