结论:

insert():插入记录并将同步更新到session缓存。

update():更新记录并同步更新到session缓存。

delete():删除记录并同步更新session缓存。

get():
如果缓存中存在要查找的记录,直接返回该条记录。

如果缓存中不存在要查找的记录,则执行查询语句,在数据库中查找。

load():若上次已执行过load,也查找不到该记录并抛出ObjectNotFoundException异常,则这次也会直接抛出异常,不会再执行Sql查询,而且不管在这中间是否插入了记
       录。

若上次已执行过get,也查找不到该记录并返回Null,这次依旧会执行SQL查询,若查找不到抛出ObjectNotFoundException异常。

实例:

代码:

@Test
public void test9(){
try{
test1();
Session s = sessionFactory.openSession();
System.out.println(s.get(Person.class, 1L));//get查询
System.out.println(s.get(Person.class, 1L));//get查询
System.out.println(s.load(Person.class, 1L));//load查询
System.out.println(s.load(Person.class, 2L));
System.out.println(s.get(Person.class, 2L));
System.out.println(s.get(Person.class, 3L));
System.out.println(s.get(Person.class, 3L));
try{
System.out.println(s.load(Person.class, 3L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
}
save(s, 3L);
try{
System.out.println(s.load(Person.class, 3L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
}
System.out.println(s.get(Person.class, 3L));
System.out.println(s.load(Person.class, 3L));
try{
System.out.println(s.load(Person.class, 4L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
}
save(s, 4L);
try{
System.out.println(s.load(Person.class, 4L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
}
Person p = (Person) s.get(Person.class, 4L);
System.out.println(s.get(Person.class, 4L));
System.out.println(s.load(Person.class, 4L));
p.setAge(121212);
update(s, p);
System.out.println(s.get(Person.class, 4L));
delete(s, p);
System.out.println(s.get(Person.class, 4L));
try{
System.out.println(s.load(Person.class, 4L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
}
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
}
}

日志:

Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
Person [id=1, name=张三, age=12, birthDay=2014-08-29]
Person [id=1, name=张三, age=12, birthDay=2014-08-29]
Person [id=1, name=张三, age=12, birthDay=2014-08-29]
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
Person [id=2, name=李四, age=22, birthDay=2014-08-29]
Person [id=2, name=李四, age=22, birthDay=2014-08-29]
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
null
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
null
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#3]
Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#3]
Person [id=3, name=张三, age=12, birthDay=Fri Aug 29 16:33:31 CST 2014]
Person [id=3, name=张三, age=12, birthDay=Fri Aug 29 16:33:31 CST 2014]
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]
Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]
Person [id=4, name=张三, age=12, birthDay=Fri Aug 29 16:33:31 CST 2014]
Person [id=4, name=张三, age=12, birthDay=Fri Aug 29 16:33:31 CST 2014]
Hibernate: update person set pname=?, birthDay=?, age=? where pid=?
Person [id=4, name=张三, age=121212, birthDay=Fri Aug 29 16:33:31 CST 2014]
Hibernate: delete from person where pid=?
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
null
Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]

日志分析:

@Test
public void test9(){
try{
test1();
//新增id为1,2的两条记录
//Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
//Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
Session s = sessionFactory.openSession();
System.out.println(s.get(Person.class, 1L));//get查询
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
// Person [id=1, name=张三, age=12, birthDay=2014-08-29]
//分析:执行了查询语句,查到了id为1的记录
System.out.println(s.get(Person.class, 1L));//get查询
//日志输出:Person [id=1, name=张三, age=12, birthDay=2014-08-29]
//分析: 没有执行查询语句,直接获取了结果,说明上次get获取的结果已存入session,而这次get发现session中已存在id为1的记录,直接返回
System.out.println(s.load(Person.class, 1L));//load查询
//日志输出:Person [id=1, name=张三, age=12, birthDay=2014-08-29]
//分析:没有执行查询语句,直接获取了结果,说明上次get获取的结果已存入session,而这次load发现session中已存在id为1的记录,直接返回
System.out.println(s.load(Person.class, 2L));
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
// Person [id=2, name=李四, age=22, birthDay=2014-08-29]
//分析:session中没有id为2的记录,执行查询语句,找到了id为2的记录
System.out.println(s.get(Person.class, 2L));
//日志输出:Person [id=2, name=李四, age=22, birthDay=2014-08-29]
//分析:没有执行查询语句,直接获取了结果,说明上次load获取的结果已存入session,而这次get发现session中已存在id为2的记录,直接返回
System.out.println(s.get(Person.class, 3L));
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
//null
//分析:查询session缓存中没找到id为3的记录,执行查询语句,也没有查到id为3的记录,返回Null
System.out.println(s.get(Person.class, 3L));
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
// null
//分析:在session中没有找到id为3的记录,再次执行查询语句,也没有查到id为3的记录,返回Null
try{
System.out.println(s.load(Person.class, 3L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
// ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]
//分析:查询缓冲不存在id为3的记录,执行查询语句,在数据库中也未发现id为3的记录,抛出异常,说明未采用上次get的结果
}
save(s, 3L);
//日志输出:Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
//分析:执行插入记录的语句
try{
System.out.println(s.load(Person.class, 3L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
//日志输出:ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]
//分析:查询缓冲不存在id为3的记录,但未执行查询语句,抛出异常,说明直接采用了上次load的结果
}
System.out.println(s.get(Person.class, 3L));
//Person [id=3, name=张三, age=12, birthDay=2014-08-29]
//分析:缓冲存在id为3的记录,直接返回。说明save时已经更新了缓存中的记录
System.out.println(s.load(Person.class, 3L));
//日志输出:Person [id=3, name=张三, age=12, birthDay=2014-08-29]
//分析:缓存中存在id为3的记录,直接返回,不执行查询语句
try{
System.out.println(s.load(Person.class, 4L));
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
//分析:缓存中不存在id为4的记录,执行查询语句
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
//日志输出:ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]
//分析:在数据库中也未查到相应的记录,抛出异常
}
save(s, 4L);
//日志输出:Hibernate: insert into person (pname, birthDay, age, pid) values (?, ?, ?, ?)
//分析:保存id为4的记录
try{
System.out.println(s.load(Person.class, 4L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
//日志输出:ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]
//分析:缓存中为找到id为4的记录,但也未执行查询语句,而是直接返回,说明,该次load直接采用了上次load的结果
}
Person p = (Person) s.get(Person.class, 4L);
System.out.println(s.get(Person.class, 4L));
//日志输出:Person [id=4, name=张三, age=12, birthDay=2014-08-29]
//分析:缓存中找到id为4的记录,直接返回,不执行查询语句。
System.out.println(s.load(Person.class, 4L));
//日志输出:Person [id=4, name=张三, age=12, birthDay=2014-08-29]
//分析:缓存中存在id为4的记录,直接返回,不执行查询语句
p.setAge(121212);
update(s, p);
System.out.println(s.get(Person.class, 4L));
//日志输出:Person [id=4, name=张三, age=121212, birthDay=2014-08-29]
//分析:没有执行查询语句,说明update时也同步更新了缓存中的记录
delete(s, p);
//日志输出:Hibernate: delete from person where pid=?
//分析:执行删除语句
try{
System.out.println(s.get(Person.class, 4L));
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
// null
//分析:在缓存中没有找到id为4的记录,执行查询语句,也未找到记录,返回Null
}
try{
System.out.println(s.load(Person.class, 4L));
//日志输出:Hibernate: select person0_.pid as pid0_0_, person0_.pname as pname0_0_, person0_.birthDay as birthDay0_0_, person0_.age as age0_0_ from person person0_ where person0_.pid=?
//分析: 缓存中未找到id为4的记录,执行查询语句
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
//日志输出:ObjectNotFoundException: No row with the given identifier exists: [hibernate.entity.Person#4]
//分析: 在数据库中也未找到id为4的记录,直接抛出异常
}
}catch(Exception e){
System.out.println(ExceptionUtils.getMessage(e));
}
}

Hibernate学习之get和load区别的更多相关文章

  1. Hibernate学习笔记(一)

    2016/4/18 19:58:58 Hibernate学习笔记(一) 1.Hibernate框架的概述: 就是一个持久层的ORM框架. ORM:对象关系映射.将Java中实体对象与关系型数据库中表建 ...

  2. Hibernate学习笔记(二)

    2016/4/22 23:19:44 Hibernate学习笔记(二) 1.1 Hibernate的持久化类状态 1.1.1 Hibernate的持久化类状态 持久化:就是一个实体类与数据库表建立了映 ...

  3. Hibernate学习之面试问题汇总

    1. Hibernate 的检索方式有哪些 ? ① 导航对象图检索 ② OID检索 ③ HQL检索 ④ QBC检索 ⑤ 本地SQL检索 2. 在 Hibernate 中 Java 对象的状态有哪些 ? ...

  4. Hibernate学习---缓存机制

    前言:这些天学习效率比较慢,可能是手头的事情比较多,所以学习进度比较慢. 在之前的Hibernate学习中,我们无论是CURD,对单表查询还是检索优化,我们好像都离不开session,session我 ...

  5. Hibernate学习之——搭建log4j日志环境

    昨天讲了Hibernate开发环境的搭建以及实现一个Hibernate的基础示例,但是你会发现运行输出只有sql语句,很多输出信息都看不见.这是因为用到的是slf4j-nop-1.6.1.jar的实现 ...

  6. hibernate学习(7)——HQL查询

    1.HQL查询定义 Hibernate查询分类: 1. get/load 根据OID检索 2. 对象视图检索 c.getOrders 3. Sql语句 createSqlQuery 4. Hql语句 ...

  7. Hibernate 学习笔记一

    Hibernate 学习笔记一 今天学习了hibernate的一点入门知识,主要是配置domain对象和表的关系映射,hibernate的一些常用的配置,以及对应的一个向数据库插入数据的小例子.期间碰 ...

  8. hibernate中@Entity和@Table的区别

    Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...

  9. Hibernate学习笔记-Hibernate HQL查询

    Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...

随机推荐

  1. VMWare Workstation 占用443端口导致apache启动不了

    中午安装vm,装linux 系统,搞了好几次才装成功,下午启动apache 忽然发现apache启动不了,各种郁闷啊,打开错误日志,NameVirtualHost无效,各种郁闷呐,试着修改端口,修改配 ...

  2. Oracle课堂实验一“表的使用”代码。

    --创建本地管理表空间CustomerTBSCREATE TABLESPACE CustomerTBS         DATAFILE 'd:\Oracle11\product\11.2.0\ora ...

  3. 加密算法 - RSA

    与DES不同,RSA算法中,每个通信主体都有两个钥匙,一个公钥一个私钥. 就是有2把钥匙1.使用publicKey可以对数据进行加密2.使用Key才能对数据进行解密单方向传输用公钥加密的数据,只有私钥 ...

  4. 安装app到Simulator

    1.安装brew 打开命令行,执行以下命令: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install ...

  5. 【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)

    [题意] 有n个绿洲, m条道路,每条路上有一个温度,和一个路程长度,从绿洲s到绿洲t,求一条道路的最高温度尽量小, 如果有多条, 选一条总路程最短的. InputInput consists of ...

  6. Android 获取SDCard上图片和视频的缩略图

    获取图片缩略图和视频缩略图的方法: Java代码: import java.io.File; import android.app.Activity; import android.graphics. ...

  7. Button with Hover Effect (Learned from 百度脑图)

    今天想学学PM的技能, 打开了百度脑图的网站, 看到中间那个按键的hover效果蛮好看, 遂学习一下. 效果如下: Demo 其实就是利用:before绘制了半透明白色的遮罩, 平时用transfor ...

  8. android发送/接收Json包含中文的处理

    转自:http://wiki.neal365.com/2013/02/25/android%E5%8F%91%E9%80%81%E6%8E%A5%E6%94%B6json%E5%8C%85%E5%90 ...

  9. bzoj1232

    由题意知,最后要保留的边肯定都要被走过 来回一条边所花费的时间=2*边长+安慰边两端的牛所要花的时间和 总时间就等于所保留边来回的时间和+根节点时间: 不难想到做一下最小生成树即可 贪心可知,根一定选 ...

  10. 改善C#程序的50种方法

    为什么程序已经可以正常工作了,我们还要改变它们呢?答案就是我们可以让它们变得更好.我们常常会改变所使用的工具或者语言,因为新的工具或者语言更富生产力.如果固守旧有的习惯,我们将得不到期望的结果.对于C ...