结论:

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. 在windows下的mysql使用

    具体可参照http://jingyan.baidu.com/article/3aed632e19b5e8701080918f.html 1.安装mysql. 直接百度搜索mysql下载.

  2. 【WPF】Dispatcher及线程操作

    WPF 应用程序启动后,会有两个线程: 1. 一个是用来处理UI呈现(处理UI的请求,比如输入和展现等操作). 2. 一个用来管理 UI的 (对UI元素及整个UI进行管理). 像Winform一样,W ...

  3. WARNING L15: MULTIPLE CALL TO SEGMENT

    原网页:http://www.cnblogs.com/CuriosityWzk/archive/2011/12/25/2301090.html WARNING L15: MULTIPLE CALL T ...

  4. http://www.w3cplus.com/animation/create-animated-text-fills.html

    关于svg的资料: http://www.w3cplus.com/animation/create-animated-text-fills.html asp.net中jquery的ajax调用cs文件 ...

  5. JENKINS里,如何为SLAVE配置多个不同的JAVA环境?

    今天遇到这个问题了, 原来在MASTER配置里可以统一管理的,不管这个路径有没有在MASTER上. 这样一来,JENKINS在编译时,会优先选用环境变量里的JAVA版本,然后才是MAVEN里的JAVA ...

  6. gdb调试高级用法

    Linux下进程崩溃时定位源代码位置 如何在调试内核时,同时可以调试应用程序的做法: (cskygdb) c Continuing. ^C Program received signal SIGINT ...

  7. 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique

    2016-10-09 23:14:43.177 DEBUG [restartedMain][org.springframework.core.type.classreading.AnnotationA ...

  8. 利用if else咱们结婚吧

    class Program    {        static void Main(string[] args)        {            while (true)           ...

  9. ♫【CSS】命名颜色

    color: aqua; 每个浏览器都支持一套它能识别且能正确渲染的命名颜色.这些颜色包括: aqua 浅绿色 #00ffffblack 黑色 #000000blue 蓝色 #0000fffuchsi ...

  10. VM Depot 喜迎中国本土开源镜像!

     发布于 2014-04-07 作者 陈 忠岳 VM Depot 登陆中国之际,我非常高兴地告诉大家,一批各位耳熟能详的中国本地开源镜像已同时上线!得益于开源社区的大力支持,Ubuntu 麒麟13 ...