`

从源码层面来看load与get的区别(一): Hibernate源码研究碎得(5)

阅读更多


在写以前先跟着源码的执行大致地看了看这两个方法所涉及的东西,一下子感觉到对这两个方法的比较将对以后session里的众多方法乃至整个Hibernate的研究都会起到很大的促进作用,由于这两个方法所涉及的东西很多,估计能写出10篇左右的Blog,我也就不用担心这些天写Blog没什么要出手的了.

**************************************************

源码级地来看load与get的区别(一):

load and get:

引用<Java Persistence with Hibernate>中的描述,我们得知load与get方法有如下区别:
1,how they indicate that the instance could not be found.如果在DB中没有找到与给定的identifier吻合的记录时,get返回null;而load方法将抛出一个RuntimeException类型的ObjectNotFoundException. So, it's your choice what error-handling you perfer.

这个理解上没什么困难,在实际的应用中也没啥难度.

2,与上面的第一条相比更为重要是,load方法会返回一个proxy(a placeholder,BTW,这个placeholder是我对proxy一词理解的一个换词式的解释,尽管中文里对proxy的翻译是"代理",但总觉得不对味),这种情况下并没有实际地去DB中去取相应的记录.(我的疑问:没有实际的到DB中去取,那这个Proxy是从哪获得的?如何得到?Cache中?这样从Cache中得到的Proxy有什么不好的地方?除了那个很明显的可能与DB中数据不符外?)A consequence of this is that you may get an ObjectNotFoundException later, as soon as you try to access the returned placeholder and force
its initialization(我的疑问:这个得到的Proxy又会是怎么地初始化呢?上面说access the returned placeholder还好理解些,毕竟有些属性可能没有真正的值,不过即使真正的没值也不必抛出ObjectNotFoundException吧?大不了返回一个null.  是什么原因使这个proxy在初始化时抛出ObjectNotFoundException呢?)(this is also called lazy loading; we discuss load optimization in later
chapters.)

下面这句话可以很好地帮助我们来理解为什么load返回Proxy是有大大的好处的.毕竟我们会不由自主去想:Hibernate返回一个Proxy来应付差事有些不厚道!
You may ask why this option is useful—after all, you retrieve an object to access it. It’s common to obtain a persistent instance to assign it as a reference to another instance. For example, imagine that you need the item only for a single purpose: to set an association with a Comment: aComment.setForAuction(item).If this is all you plan to do with the item, a proxy will do fine; there is no need to
hit the database. In other words, when the Comment is saved, you need the foreign key value of an item inserted into the COMMENT table. The proxy of an Item provides just that: an identifier value wrapped in a placeholder that looks like the real thing.

理解是上这样了,下一篇中小菜我将试着结合源码来分析理解这两者间的区别.

3
0
分享到:
评论
2 楼 xinshaoye 2009-01-06  
load的实现是使用代理,使用了cglib
1 楼 jindw 2008-05-14  
这个问题很晦涩阿。
load 其实应该是lazyLoad。我感觉这个名字取得不好。

相关推荐

    hibernate 三种 查询 方式 load与get的区别

    NULL 博文链接:https://cici9898-007.iteye.com/blog/1847710

    hibernate中get和load方法的区别

    NULL 博文链接:https://ysj5125094.iteye.com/blog/1897557

    传智播客李勇hibernate源码1-20课

    传智播客李勇hibernate源码1-20课,目录如下:01_hibernate介绍与动手入门体验;02_hibernate入门案例的细节分析; 03_hibernate入门案例的代码优化; 04_Session接口及get|load|persist方法 05_实体对象的三种状态...

    Hibernate的get和load方法的区别

    NULL 博文链接:https://811417208-qq-com.iteye.com/blog/506950

    Hibernate查询 load与get的区别及其它查询测试

    NULL 博文链接:https://quicker.iteye.com/blog/658451

    hibernate get/load/saveOrUpdate/statistics

    博文链接:https://log-cd.iteye.com/blog/205166

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    ssh(structs,spring,hibernate)框架中的上传下载

     需要指定的是Spring 1.2.5提供了两套Hibernate的支持包,其中Hibernate 2相关的封装类位于org.springframework.orm.hibernate2.*包中,而Hibernate 3.0的封装类位于org.springframework.orm.hibernate3.*包中,...

    DWR.xml配置文件说明书(含源码)

    DWR有个专门用于解析上面配置语句的解析器,虽然上面的是JDK5中才有的特性,因为有解析器的原因这也可以应用与JDK5之前的版本. 解析规则是不可见的,但有两种例外情况. 一种情况是因为DWR1.0的解析器中有个Bug,在有些...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

    cms后台管理

    一 Jeecms安装过程 将解压后得到的jeecms-3.0.2-final文件夹下的root文件夹更名为jeecms拷贝到tomcat 安装目录下的webapps 文件夹下(例如: D:\Tomcat 6.0\webapps\),启动tomcat,在地址栏中输入...

Global site tag (gtag.js) - Google Analytics