Hibernate中load()和get()的区别java
Hibernate中Session接口提供的load()和get()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别,得到机制有所不一样。综合课堂和资料查阅总结以下:数据库
若是使用get方法,hibernate会去确认该id对应的数据是否存在,它首先会去session中去查询(session缓存其实就hibernate的一级缓存),若是没有,再去二级缓存中去查询,若是再没有,就去数据库中查询,仍然没有找到的话,就返回null缓存
而使用load方法的话,hibernate会认定该id对应的数据必定存在,它也会先去session缓存中去查找,session
若是没有找到,hibernate会根据lazy属性值来肯定是否使用延迟加载。若是lazy=‘true’ ,就使用延迟加载,返回该代理对象,ide
等到真正访问到该对象的属性时才会去二级缓存中查询,若是没有,再去数据库中查询,若是尚未,就抛出org.hibernate.ObjectNotFoundException异常。性能
若是lazy='false' 则不使用延迟加载,这时load的访问机制就和get同样了。测试
课堂代码举例说明:spa
代码演示 dao类: public interface IUserDao { public SysUserEntity findUserById(String id); }
Impl: (load()方法): public class UserDaoImpl implements IUserDao{ public SysUserEntity findUserById(String id) { Session session= HibernateSessionFactory.getSession(); //获得持久状态的对象 SysUserEntity user=(SysUserEntity)session.load(SysUserEntity.class,id); return user; } }
测试:(查找id为1和100的用户名,id为1的存在,100不存在) //(id=”1”) public class Test { public static void main(String[] args) { IUserDao dao=new UserDaoImpl(); SysUserEntity user=dao.findUserById("1"); System.out.println(user.getUser()); } } 测试以下: Console: Hibernate: select sysuserent0_.userId as userId1_2_0_, sysuserent0_.loginPassWord as loginPas2_2_0_, sysuserent0_.loginUser as loginUse3_2_0_, sysuserent0_.user as user4_2_0_ from sys_user sysuserent0_ where sysuserent0_.userId=? 张三 //(id=”100”) public class Test { public static void main(String[] args) { IUserDao dao=new UserDaoImpl(); SysUserEntity user=dao.findUserById("100"); System.out.println(user); } } 测试以下: Hibernate: select sysuserent0_.userId as userId1_2_0_, sysuserent0_.loginPassWord as loginPas2_2_0_, sysuserent0_.loginUser as loginUse3_2_0_, sysuserent0_.user as user4_2_0_ from sys_user sysuserent0_ where sysuserent0_.userId=? Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.lovo.h.entity.SysUserEntity#100]
get()方法 public class UserDaoImpl implements IUserDao{ public SysUserEntity findUserById(String id) { Session session= HibernateSessionFactory.getSession(); //获得持久状态的对象 SysUserEntity user=(SysUserEntity)session.get(SysUserEntity.class,id); //关闭session HibernateSessionFactory.closeSession(); return user; } }
测试 //(id=”1”) public class Test { public static void main(String[] args) { IUserDao dao=new UserDaoImpl(); SysUserEntity user=dao.findUserById("1"); System.out.println(user.getUser()); } } 测试以下: Hibernate: select sysuserent0_.userId as userId1_2_0_, sysuserent0_.loginPassWord as loginPas2_2_0_, sysuserent0_.loginUser as loginUse3_2_0_, sysuserent0_.user as user4_2_0_ from sys_user sysuserent0_ where sysuserent0_.userId=? 张三 //(id=”100”) //(id=”100”) public class Test { public static void main(String[] args) { IUserDao dao=new UserDaoImpl(); SysUserEntity user=dao.findUserById("100"); System.out.println(user); } } 测试以下: Hibernate: select sysuserent0_.userId as userId1_2_0_, sysuserent0_.loginPassWord as loginPas2_2_0_, sysuserent0_.loginUser as loginUse3_2_0_, sysuserent0_.user as user4_2_0_ from sys_user sysuserent0_ where sysuserent0_.userId=? null
总之get和load的根本区别,hibernate对于load方法认为该数据在数据库中必定存在,能够放心的使用代理来延迟加载,若是在使用过程当中发现了问题,只能抛异常;而对于get方法,hibernate必定要获取到真实的数据,不然返回null。get方法首先查询session缓存,没有的话查询二级缓存,最后查询数据库;反而load方法建立时查询session缓存,没有就建立代理,实际使用数据时才查询二级缓存和数据库。hibernate