Java笔记之hibernate(十一):get和load的区别
0.说在前面
1.新建GetAndLoadTest类
package com.hibernate.demo.test; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import com.hibernate.demo.bean.Employee; public class GetAndLoadTest { public static void main(String[] args) { //加载配置文件,创建会话工厂对象 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); //创建会话对象 Session session = sessionFactory.openSession(); Session session2 = sessionFactory.openSession(); System.out.println("log1"); //使用get方法获取对象 Employee employee = (Employee) session.get(Employee.class, 2); System.out.println("log2"); System.out.println(employee.getEmpName()); System.out.println("log3"); //使用load方法获取对象 Employee employee2 = (Employee) session2.load(Employee.class, 2); System.out.println("log4"); System.out.println(employee2.getEmpName()); System.out.println("log5"); System.out.println("======华丽的分割线======"); Employee employee3 = (Employee) session.get(Employee.class, 100); System.out.println(employee3); System.out.println("log6"); Employee employee4= (Employee) session2.load(Employee.class, 100); System.out.println(employee4); //关闭会话对象 session.close(); //关闭会话工厂对象 sessionFactory.close(); } }
2.运行GetAndLoadTest类
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. log1 Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_, employee0_.dept_id as dept3_0_0_ from t_employee employee0_ where employee0_.emp_id=? Hibernate: select department0_.dept_id as dept1_2_0_, department0_.dept_name as dept2_2_0_ from t_department department0_ where department0_.dept_id=? Hibernate: select employees0_.dept_id as dept3_2_1_, employees0_.emp_id as emp1_1_, employees0_.emp_id as emp1_0_0_, employees0_.emp_name as emp2_0_0_, employees0_.dept_id as dept3_0_0_ from t_employee employees0_ where employees0_.dept_id=? log2 李四 log3 log4 Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_, employee0_.dept_id as dept3_0_0_ from t_employee employee0_ where employee0_.emp_id=? Hibernate: select department0_.dept_id as dept1_2_0_, department0_.dept_name as dept2_2_0_ from t_department department0_ where department0_.dept_id=? Hibernate: select employees0_.dept_id as dept3_2_1_, employees0_.emp_id as emp1_1_, employees0_.emp_id as emp1_0_0_, employees0_.emp_name as emp2_0_0_, employees0_.dept_id as dept3_0_0_ from t_employee employees0_ where employees0_.dept_id=? 李四 log5 ======华丽的分割线====== Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_, employee0_.dept_id as dept3_0_0_ from t_employee employee0_ where employee0_.emp_id=? null log6 Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.emp_name as emp2_0_0_, employee0_.dept_id as dept3_0_0_ from t_employee employee0_ where employee0_.emp_id=? Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.hibernate.demo.bean.Employee#100] at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:433) at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:189) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:178) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190) at com.hibernate.demo.bean.Employee_$$_jvst351_0.toString(Employee_$$_jvst351_0.java) at java.lang.String.valueOf(String.java:2994) at java.io.PrintStream.println(PrintStream.java:821) at com.hibernate.demo.test.GetAndLoadTest.main(GetAndLoadTest.java:34)
3.总结
(1).从log1与log2之间的信息可以看出,使用get方法查询对象信息,是没有延迟加载的,直接发送SQL语句进行数据库查询;
(2).从log3与log4之间可以看出,load方法并没有直接发送SQL语句去数据库查询,而是在log4与log5直接访问对象的属性信息时才发送SQL去数据库查询,是有延迟加载的,也就是属性延迟加载;
(3).从log5与log6之间的信息可以看出,get方法在查询不存在的对象信息时返回的是null;
(4).从log6之后的信息可以看出,load方法在查询不存在的对象信息时会报异常;