Hibernate学习之get和load区别
结论:
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)); } }