hibernate 中 load和get的测试

来源:互联网 发布:村淘宝服务站怎样赚钱 编辑:程序博客网 时间:2024/05/18 00:43

之前看到的很多书本的说法都是:
hibernate对于load方法认为该数据在数据库中一定存在,可以放心的使用代理来延迟加载,如果在使用过程中发现了问题,只能抛异常;而对于get方法,hibernate一定要获取到真实的数据,否则返回null

之后我测试了下,查询不存在的对象,然后打印其属性
Kuser k = (Kuser)session.load(Kuser.class,300);
//Kuser k = (Kuser)session.get(Kuser.class,3000);
System.out.println(k.getKusername());
load报:
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.koala.pojo.Kuser#300]
get报:
Exception in thread "main" java.lang.NullPointerException

 

所以:

使用了不存在的数据后,

load是ObjectNotFoundException

get是NullPointerException