Hibernate中load()和get()的区别
Hibernate中load()和get()的区别
Hibernate中Session接口提供的load()和get()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别,获得机制有所不同。综合课堂和资料查阅总结如下:
如果使用get方法,hibernate会去确认该id对应的数据是否存在,它首先会去session中去查询(session缓存其实就hibernate的一级缓存),如果没有,再去二级缓存中去查询,如果再没有,就去数据库中查询,仍然没有找到的话,就返回null
而使用load方法的话,hibernate会认定该id对应的数据一定存在,它也会先去session缓存中去查找,
如果没有找到,hibernate会根据lazy属性值来确定是否使用延迟加载。如果lazy=‘true’ ,就使用延迟加载,返回该代理对象,
等到真正访问到该对象的属性时才会去二级缓存中查询,如果没有,再去数据库中查询,如果还没有,就抛出org.hibernate.ObjectNotFoundException异常。
如果lazy='false' 则不使用延迟加载,这时load的访问机制就和get一样了。
课堂代码举例说明:
代码演示
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缓存,没有就创建代理,实际使用数据时才查询二级缓存和数据库。