当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:12:00  来源:本站整理

Hibernate中outerjoin、fetch join的操纵[Java编程]

赞助商链接



  本文“Hibernate中outerjoin、fetch join的操纵[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

1、outer-join关键字(many-to-one的情形)

outer-join关键字有3个值,辨别是true,false,auto,默许是auto.

true: 表示利用外衔接抓取关联的内容,这里的意思是当利用load(OrderLineItem.class,"id")时,Hibernate只生成一条SQL语句将OrderLineItem与他的父亲Order全部初始化.

select * from OrderLineItem o left joinOrderpon o.OrderId=p.OrderId where o.OrderLineItem_Id=?

false:表示不利用外衔接抓取关联的内容,当load(OrderLineItem.class,"id")时,Hibernate生成两条SQL语句,一条查询OrderLineItem表,另一条查询Order表.这样的好处是可以设置耽误加载,此处要将Order类设置为lazy=true.

select * from OrderLineItem owhere o.OrderLineItem_Id=?

select * from Order p where p.OrderId=?

auto:具体是ture还是false看hibernate.cfg.xml中的配置

注意:假如利用HQL查询OrderLineItem,如 from OrderLineItem o where o.id='id',老是不利用外部抓取,及outer-join失效.

2、outer-join(调集)

由于调集可以设置lazy="true",所以lazy与outer-join不能同时为true,当lazy="true"时,outer-join将一向是false,假如lazy="false",则outer-join用法与1同

3、HQL语句会将POJO配置文件中的关联一并查询,即便在HQL语句中没有明确join.

4、In HQL, the "fetch join" clause can be used for per-query specific outer join fetching. One important thing many people miss there, is that HQL queries will ignore the outer-join attribute you specified in your mapping. This makes it possible to configure the default loading behaviour of session.load() and session.get() and of objects loaded by navigating relationship. So if you specify

and then do

MyObject obj = session.createQuery("from MyObject").uniqueResult();

obj.getMySet().iterator().next();

you will still have an additional query and no outer-join. So you must explicily request the outer-join fetching:

MyObject obj = session.createQuery(

"from MyObject mo left join fetch mo.mySet").uniqueResult();

obj.getMySet().iterator().next();

Another important thing to know is that you can only fetch one collection reference in a query. That means you can just use one fetch join. You can however fetch "one" references in addition, as this sample from the Hibernate Docs demonstrates:

from eg.Cat as cat

inner join fetch cat.mate

left join fetch cat.kittens

We have once considered lifting this limitation, but then decided against it, because using more than one fetch-join would be a bad idea generally: The generated ResultSet becomes huge and is a major performance loss.

So alltogether the "fetch join" clause is an important instrument Hibernate users should learn how to leverage, as it allows tuning the fetch behaviour of a certain use case.

5、join fetch 与 join 的辨别

假如HQL利用了衔接,但是没有利用fetch关键字,则生成的SQL语句固然有衔接,但是并没有取衔接表的数据,还是需求单独的sql取数据,也就是 select a,b,d...中没有衔接表的字段

6、假如调集被声明为lazy=true,在HQL中假如显式的利用 join fetch 则耽误加载失效.

7、在one-to-many的one端显式设置fecth="join",则无论若何都采纳预先抓取(生成一个SQl),耽误加载失效(生成两个SQL)

8、many-to-one的耽误加载是在配置文件的class标签设置lazy="true",one-to-many和many-to- many的耽误加载是在set标签中设置lazy="true".而one-to-one不只要在calss标签设置lazy="true",并且要在 one-to-one标签中设置constrained="true".


  以上是“Hibernate中outerjoin、fetch join的操纵[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 在Hibernate里面动态切换SChema实现访谒差别的数据库的几种办法
  • Hibernate 3新增XML关系长期性介绍
  • Hibernate配置文件在单元测试中的操纵
  • 在Hibernate中动态切换Schema
  • hibernate annoation (八 关联映射)
  • hibernate annoation (九 cascading)
  • <b>hibernate annoation (十 映射查询)</b>
  • hibernate annoation(十一 缓存Ehcache 采纳annoation)
  • Hibernate:操作配置文件生成数据库
  • hibernate annoation (一 加载)
  • hibernate annoation (二 成立表)
  • <b>hibernate annoation (三 id生成器)</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .