结论:

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. 新浪微博登录接口(PHP版)

    CI框架下 新浪微博登录接口完整版说明:本贴只适合CI框架.功能实现:登录接口跳转链接成功,获取用户信息(包括最重要的u_id)成功,将用户与本地平台连接起来,用户登录成功后信息的存储,本地数据库第三 ...

  2. JNI/NDK开发指南(开山篇)

    转载请注明出处:http://blog.csdn.net/xyang81/article/details/41759643 相信很多做过Java或Android开发的朋友经常会接触到JNI方面的技术, ...

  3. BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛

    Description FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:).在赛场上,奶牛们按1..N依次编号.每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平 ...

  4. mysql Partition(分区)初探

    mysql Partition(分区)初探   表数据量大的时候一般都考虑水平拆分,即所谓的sharding.不过mysql本身具有分区功能,可以实现一定程度 的水平切分.  mysql是具有MERG ...

  5. 【codeforce 219D】 Choosing Capital for Treeland (树形DP)

    Choosing Capital for Treeland Description The country Treeland consists of n cities, some pairs of t ...

  6. 【BZOJ 1010】 [HNOI2008]玩具装箱toy (斜率优化)

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9330  Solved: 3739 Descriptio ...

  7. 几个RTP的开源实现

    玩了两天rtp协议,基本把rtsp/rtcp/rtp/rtmp/srtp/strcp/mms,几个协议的区别和概念弄明白了. 这里记录一下. rtsp:类似用户界面操作,和Http比较类似,提供播放, ...

  8. [转贴] C++内存管理检测工具 Valgrind

    用C/C++开发其中最令人头疼的一个问题就是内存管理,有时候为了查找一个内存泄漏或者一个内存访问越界,需要要花上好几天时间,如果有一款工具能够帮助我们做这件事情就好了,valgrind正好就是这样的一 ...

  9. pcDuino汉化方法

    1,打开终端:2,在终端输入命令 sudo apt-get update 更新一下软件源3, 输入命令下载中文支持包 sudo apt-get install language-pack-gnome- ...

  10. 编程实现改变win7主题

    一  : 解析问题 1. Windows 7 主题在:%windir%\Resources\Themes  : 2: 我们通过shell 命令  (这个是msdn中提到的) rundll32.exe ...