getHibernateTemplate.load() 和get()之间的区别

来源:互联网 发布:东罗马帝国 知乎 编辑:程序博客网 时间:2024/06/10 01:28
主要的地方:getHibernateTemplate.load() 存在延迟加载问题。
getHibernateTemplate.get() 不存在此问题,她是不采用lazy机制的。1 当记录不存在时候,get方法返回null,load方法产生异常,即get()可以取空的数据集,但load()不行。 
       take a look at the Hibernate documentation (though I agree is not very explicit)--the HibernateTemplate is basically a wrapper around the native Hibernate API.
       get() will return null if an object is not found while load() will always return a   non-null object which is a proxy. If the underlying object does not exist, the proxy will thrown ObjectNotFoundException.
       load() should be used when you are sure that the object exits while get() when
you're not. 2 load方法可以返回实体的代理类,get方法则返回真是的实体类3 load方法可以充分利用hibernate的内部缓存和二级缓存中的现有数据,而get方法仅仅在内部缓存中进行数据查找,如果没有发现数据則将越过二级缓存,直接调用SQL查询数据库。4 也许别人把数据库中的数据修改了,load如何在缓存中找到了数据,则不会再访问数据库,而get则会返回最新数据。
原创粉丝点击