结论:

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. git push用法和常见问题分析

    在使用git 处理对android的修改的过程之中总结的.但不完善 Git push $ git push origin test:master         // 提交本地test分支作为远程的m ...

  2. 警惕arm-linux-gcc编译器优化选项

    arm-linux-gcc的优化选项例如(-O2),可以加速我们的程序,使程序执行效率更高.但是,倘若我们就是需要程序慢一点运行,但是优化却把我们的延时函数优化的没有了的时候,这种优化却不是我们想要的 ...

  3. DateTimePicker:jQuery日期和时间插件

    点击在线预览效果       点击下载该插件 下面是效果截图:

  4. scanf格式控制符

    格式控制 . %d %o %x %c %s %f %e 无%u格式.%g格式 . scanf("%3d%3d", &a, &b); 输入: //a=123,b=45 ...

  5. git操作回顾:

    1. git查看自己的本地分支: ***:~/mysite/mysite$ git branch * master 2. 查看远程分支: ***:~/mysite/mysite$ git branch ...

  6. 容斥原理算法总结(bzoj 2986 2839)

    容斥原理是一个从小学就开始学习的算法.但是很多难题现在都觉得做的十分吃力. 容斥原理大概有两种表现形式,一种是按照倍数进行容斥,这个东西直接用莫比乌斯函数就可以了. #include<iostr ...

  7. Painting The Wall 期望DP Codeforces 398_B

    B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. linux 监控

    http://www.iyunv.com/thread-50606-1-1.html http://segmentfault.com/a/1190000002537665 http://blog.cs ...

  9. AsyncHttpClient 开源框架學習研究

    转载请注明出处:http://blog.csdn.net/krislight OverView: AsyncHttpClient庫 基於Apache的HttpClient框架,是一個異步的httpCl ...

  10. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...