`
citygu
  • 浏览: 26864 次
  • 性别: Icon_minigender_1
  • 来自: CZ
社区版块
存档分类
最新评论

hibernateTemplate load get区别

    博客分类:
  • JAVA
SQL 
阅读更多
get:Return the persistent instance of the given entity class with the given identifier, or

    null if there is no such persistent instance. (If the instance, or a proxy for the
    instance, is already associated with the session, return that instance or proxy.)
load:Return the persistent instance of the given entity class with the given identifier,
     assuming that the instance exists.You should not use this method to determine

     if an instance exists (use <tt>get</tt> * instead). Use this only to retrieve

     an instance that you assume exists, where non-existence would be an actual error.
区别:

(1)如果未能发现符合条件的记录,get方法返回null,而load方法会抛出一个ObjectNotFoundException。
(2)load()方法可返回实体的代理类实例;而get方法永远直接返回实体类。
(3)load方法可以充分利用内部缓存(session缓存)和二级缓存中的现有数据,它将会使用代理来延迟加载该对象,然后当用到对象中的其他属性数据时才查询数据库,查找方式:load时会先查一下session缓存看看该id对应的对象是否存在,不存在则创建代理。所以当id对应的记录在据库中存在就可以使用load方法来实现延迟加载;而get方法则仅仅在内部缓存中进行数据查找,如没有发现对应数据,将越过二级缓存,直接调用SQL完成数据读取。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics