`

Hibernate fetch lazy cascade inverse 关键字

阅读更多
  假设有两个表,Photos(一)   ---   picture(多)Photo包含picture集合

结论1: HQL代码 > fetch(配置) > lazy (配置)
结论2: 默认 lazy="true"
结论3: fetch 和 lazy 主要是用来级联查询的,    而 cascade 和 inverse 主要是用来级联插入和修改的
结论4: 如果你是用spring来帮你管理你的session, 并且是自动提交,延迟加载就等于没加载~_~(当然
                 除非你手动重新打开session然后手动Hibernate.initialize(set);然后关闭session.
结论5:      cascade主要是简化了在代码中的级联更新和删除。
结论6:老爸可以有多个孩子,一个孩子不能有多个老爸,而且老爸说的算, 孩子围着老爸转。
                所以Photos老爸要有权力所以 cascade 这个关键子都是送给老爸的, 也就是级联更新,
                老爸改姓了,儿子也得跟着改,呵呵。“不然,就没有零花钱咯”。
                 而Picture儿子整体挨骂,但是还是要维护父子之间良好的关系,对老爸百依百顺,所
                以老爸就说,儿子,“关系,由你来维护(inverse="true") ,不然就不给零花钱。呵。”。
                <set name="pictures" inverse="true" cascade="all">
                     <key>
                        <column name="photosid" not-null="true" />
                     </key>
                  <one-to-many class="girl.domain.Picture" />
              </set>
               
测试代码:

    Photos p = ps.getById(1);
   Set<Picture> set = p.getPictures();
   for(Picture pic : set){
      System.out.println(pic.getId());
   }

   配置文件的一部分:
        <set name="pictures" inverse="true" cascade="all" >
             <key>
                 <column name="photosid" not-null="true" />
             </key>
             <one-to-many class="girl.domain.Picture" />
         </set>

测试过程会对配置文件不断修改:并且从来不曾手动重新打开session

测试结构:

当配置条件为 lazy=true 一句查询 测试代码中没有调用getPicture()   正常
Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

lazy=true 一句查询 有getPicture()
Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?


lazy=true 一句查询   有getPicture() 并且访问了里面的元数Picture 且有异常抛出
Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?


lazy="false" 两句查询   肯定没问题,因为全部数据都个查了出来 所以怎么调用都正常
Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?
Hibernate: select pictures0_.photosid as photosid1_, pictures0_.id as id1_, pictures0_.id as id2_0_, pictures0_.photosid as photosid2_0_, pictures0_.name as name2_0_, pictures0_.clicked as clicked2_0_, pictures0_.uploaddate as uploaddate2_0_, pictures0_.size as size2_0_, pictures0_.description as descript7_2_0_, pictures0_.uri as uri2_0_ from super.picture pictures0_ where pictures0_.photosid=?


fetch="join"   一句查询   效果 == lazy="false" 呵呵,哪个效率高,我就不知道了。。。。。。。。。。。
Hibernate: select photos0_.id as id0_1_, photos0_.userid as userid0_1_, photos0_.typeid as typeid0_1_, photos0_.name as name0_1_, photos0_.createtime as createtime0_1_, photos0_.description as descript6_0_1_, photos0_.faceid as faceid0_1_, photos0_.uri as uri0_1_, pictures1_.photosid as photosid3_, pictures1_.id as id3_, pictures1_.id as id2_0_, pictures1_.photosid as photosid2_0_, pictures1_.name as name2_0_, pictures1_.clicked as clicked2_0_, pictures1_.uploaddate as uploaddate2_0_, pictures1_.size as size2_0_, pictures1_.description as descript7_2_0_, pictures1_.uri as uri2_0_ from super.photos photos0_ left outer join super.picture pictures1_ on photos0_.id=pictures1_.photosid where photos0_.id=?

不加fetch="join" 一句查询   没有getPicture() 正常
Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

不加fetch="join" 一句查询   有getPicture() 正常
Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

不加fetch="join" 一句查询 有getPicture() 并且访问里面的元素Picture的ID 有异常抛出
Hibernate: select photos0_.id as id0_0_, photos0_.userid as userid0_0_, photos0_.typeid as typeid0_0_, photos0_.name as name0_0_, photos0_.createtime as createtime0_0_, photos0_.description as descript6_0_0_, photos0_.faceid as faceid0_0_, photos0_.uri as uri0_0_ from super.photos photos0_ where photos0_.id=?

来个两兵交战 fetch="join" lazy="true"   呵呵结果,一句查询, 结构正常 所以就当lazy不存在好了。 看来fetch 是老大。、、、、、、、、、、、、、
Hibernate: select photos0_.id as id0_1_, photos0_.userid as userid0_1_, photos0_.typeid as typeid0_1_, photos0_.name as name0_1_, photos0_.createtime as createtime0_1_, photos0_.description as descript6_0_1_, photos0_.faceid as faceid0_1_, photos0_.uri as uri0_1_, pictures1_.photosid as photosid3_, pictures1_.id as id3_, pictures1_.id as id2_0_, pictures1_.photosid as photosid2_0_, pictures1_.name as name2_0_, pictures1_.clicked as clicked2_0_, pictures1_.uploaddate as uploaddate2_0_, pictures1_.size as size2_0_, pictures1_.description as descript7_2_0_, pictures1_.uri as uri2_0_ from super.photos photos0_ left outer join super.picture pictures1_ on photos0_.id=pictures1_.photosid where photos0_.id=?

分享到:
评论

相关推荐

    hibernate

    Hibernate fetch lazy cascade inverse 关键字

    Hibernate Fetch 的作用

    正确理解hibernate fetch 的作用

    Hibernate3.2学习笔记

    Hibernate环境搭建 Hibernate主要接口 Hibernate主要映射 Hibernate的lazy、fetch、cascade等策略 Hibernate性能优化

    hibernate常用关联映射源码(很实用)

    该资源包括了hibernate开发中的常见关联映射的源代码,其中包括:one2one,one2many,many2many等,还有继承映射,inverse,fetch,cascade关键字的应用等,对初学者来说,是很实用的,难度适中,很好理解

    hibernate 中 fetch=FetchType.LAZY 懒加载失败处理方法

    主要介绍了hibernate 中 fetch=FetchType.LAZY 懒加载失败处理方法,需要的朋友可以参考下

    SSH笔记-检索策略(lazy、fetch、batch-size)

    hibernate的类级别检索策略和检索策略的lazy、fetch、batch-size属性使用

    Hibernate教程15_关联关系中的CRUD Cascade_Fetch

    http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码

    hibernate总结

    &lt;set name="emps" inverse ="true" lazy="true" cascade="none" batch-size="2" fetch="join" &gt; &lt;/hibernate-mapping&gt; Hibernate映射多对一: public class Emp implements java.io.Serializable {...

    优化Hibernate性能的几点建议

    中取出的记录条数,一般设置为30、50、100.Oracle数据库的JDBC驱动默认的Fetch Size=15,设置Fetch Size设置为:30、50,性能会有明显提升,如果继续增大,超出100,性能提升不明显,反而会消耗内存。 即在...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     16.3.5 用带子查询的select语句整批量初始化orders集合(fetch属性为“subselect”)  16.3.6 迫切左外连接检索(fetch属性为“join”)  16.4 多对一和一对一关联的检索策略  16.4.1 迫切左外连接检索(fetch...

    javascript实现fetch请求返回的统一拦截

    主要介绍了javascript实现fetch请求返回的统一拦截,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    JavaScript 用fetch 实现异步下载文件功能

    本文通过实例代码给大家介绍了JavaScript 用fetch 实现异步下载文件功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下

    fetch请求超时

    fetch请求超时,在fetch的基础上做的可以设置超时时间的 javascript 插件,具体使用请查看我的博客 https://blog.csdn.net/yumikobu/article/details/80167982

    Hibernate配置各种数据源详解

    Hibernate配置各种数据源 &lt;hibernate-configuration&gt; &lt;!– 各属性的配置–&gt; &lt;!—为true表示将Hibernate发送给...”jdbc.fetch_size”&gt;50 &lt;!– 设定对数据库进行批量删除 –&gt; ”jdbc.batch_size”&gt;30&lt;/property&gt;

    西门子 TCP通信中的Fetch和Write用法

    西门子 TCP通信中的Fetch和Write用法,PLC侧无需编程,公开的通信格式

    Hibernate+中文文档

    19.1.7. 使用延迟属性抓取(Using lazy property fetching) 19.2. 二级缓存(The Second Level Cache) 19.2.1. 缓存映射(Cache mappings) 19.2.2. 策略:只读缓存(Strategy: read only) 19.2.3. 策略:读/...

    Hibernate注释大全收藏

    @Basic(fetch = FetchType.LAZY) String getDetailedComment() { ... } // persistent property @Temporal(TemporalType.TIME) java.util.Date getDepartureTime() { ... } // persistent property @Enumerated(Enum...

    FetchRobotics_FetchRobot_Fetch!_

    Fetch Robot

    hibernate3.2中文文档(chm格式)

    19.1.7. 使用延迟属性抓取(Using lazy property fetching) 19.2. 二级缓存(The Second Level Cache) 19.2.1. 缓存映射(Cache mappings) 19.2.2. 策略:只读缓存(Strategy: read only) 19.2.3. 策略:读/...

Global site tag (gtag.js) - Google Analytics