本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[J2EE]Hibernate中outer-join与lazy关键字的使用
snowrose 发表于 2006/5/25 11:33:30

Hibernate中outer-join与lazy关键字的使用,解决大家的困惑。 <hibernate-mapping>    <class  name="com.meagle.bo.Order"    table="Orders"    dynamic-update="false"   dynamic-insert="false" >        <id  name="id"  column="Order_ID"   type="int"   unsaved-value="0">            <generator class="native">            </generator>        </id>        <set   name="orderLineItems"    table="OrderLineItem"    lazy="true"    inverse="true"   cascade="save-update"     sort="unsorted">              <key   column="Order_ID" ></key>              <one-to-many   class="com.meagle.bo.OrderLineItem" />        </set>        <property   name="userName"  type="string"  update="true"  insert="true"  access="property"            column="UserName"            not-null="true"            unique="false"  />         <property      name="total"    type="double"    update="true"   insert="true"  access="property"   column="Total"            not-null="false"            unique="false"        />    </class> </hibernate-mapping><hibernate-mapping>    <class    name="com.meagle.bo.OrderLineItem"    table="OrderLineItem"    dynamic-update="false"      dynamic-insert="false" >        <id   name="id"   column="OrderLineItem_ID"   type="int"   unsaved-value="0">            <generator class="native">            </generator>        </id>        <many-to-one   name="order"   class="com.meagle.bo.Order"   cascade="none"  outer-join="auto"   update="true"            insert="true"            access="property"            column="Order_ID" />         <property  name="description"  type="string"  update="true"  insert="true"  access="property"  column="Description"      not-null="false"   unique="false" />         <property  name="lineItemPrice"    type="double"    update="true"     insert="true"     access="property"            column="LineItemPrice"            not-null="false"            unique="false"        />    </class> </hibernate-mapping> 近来一直困惑与outer-join关键字,经过多次测试之后,现总结如下: 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 join Order p on o.OrderId=p.OrderId  where o.OrderLineItem_Id=?false:表示不使用外连接抓取关联的内容,当load(OrderLineItem.class,"id")时,Hibernate生成两条SQL语句,一条查询OrderLineItem表,另一条查询Order表。这样的好处是可以设置延迟加载,此处要将Order类设置为lazy=true。select * from OrderLineItem o where 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 specifyand then doMyObject 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 则延迟加载失效。

阅读全文(2578) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)

 
 



The Neurotic Fishbowl

.: 公告




Bloginess

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.: 我的分类(专题)

首页(130)
RIA(22)
linux(3)
J2EE(24)
the mood of everyday(58)
oral English(14)
database(2)
soap(3)
java(9)
web developer center(14)


In the Bowl

.: 最新日志

倾情奉献iphone、ipod、ipho
check ipad's rotatio
css position fixed d
field validation whe
常见浏览器cookie个数和大小限制说明
人类无法抗拒的10种心理,学会它,就能控
读as3cookbook 有感
flex builder linux s
Component returned f
use tramp in emacs i


.: 最新回复

replcica watches
replcica watches
Dell spikes game sit
回复:人类无法抗拒的10种心理,学会它,
回复:人类无法抗拒的10种心理,学会它,
回复:flex builder linu
回复:人类无法抗拒的10种心理,学会它,
回复:人类无法抗拒的10种心理,学会它,
回复:致女人------摘自读者
回复:虚心接受别人的意见,谨慎改正


The Fishkeeper
blog名称:up forever
日志总数:130
评论数量:274
留言数量:4
访问次数:1949081
建立时间:2006年5月25日



Text Me

.: 留言板

签写新留言

参观
这个界面很漂亮!
我试试吧
up forever
该换了!


Other Fish in the Sea

.: 链接


http://blogger.org.cn/blog/more.asp?name=hongrui&id=16955

邢红瑞 

http://niegc.cublog.cn/  聂国聪

http://www.blogjava.net/SteelHand/ 铁手剑谱

http://blog.sina.com.cn/qianqq 糖qq

http://blog.csdn.net/misty_wish   师弟

http://www.bbxy.net/xiaoyu/ Goteet老师

http://www.chinaitpower.com    中国it动力

http://jinyuxi.blog.sohu.com/ 靳羽西




lvetica, sans-serif; MARGIN: 0px 0px 8px; PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px } DIV.blogrollmain { FONT: 12px verdana, arial, helvetica, sans-serif } BLOCKQUOTE { COLOR: #202020; FONT: 11px verdana, arial, helvetica, sans-serif } .tinyfont { FONT: 9px verdana, arial, helvetica, sans-serif } TD { COLOR: #202020; FONT-FAMILY: verdana; FONT-SIZE: 12px } .title { COLOR: #996897; FONT-FAMILY: verdana, sans-serif; FONT-SIZE: 10pt; FONT-WEIGHT: bold } .titlefont { COLOR: #996897; FONT-FAMILY: verdana, sans-serif; FONT-SIZE: 14px; FONT-STYLE: italic } .titlefontarch { BORDER-BOTTOM: 1px solid; BORDER-LEFT: 1px solid; BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; COLOR: #996897; FONT-FAMILY: verdana, sans-serif; FONT-SIZE: 14px; TEXT-ALIGN: center; WIDTH: 100% } .menu_text { COLOR: #c1ae94; FONT-FAMILY: Verdana, Arial, sans-serif; FONT-SIZE: 10px; MARGIN-BOTTOM: 1px } .links { FONT-FAMILY: Verdana, Arial, sans-serif; FONT-SIZE: 11px } .smallfont { COLOR: #202020; FONT-FAMILY: Verdana, Arial, sans-serif; FONT-SIZE: 11px; MARGIN: 0px } A:link { COLOR: #996897; TEXT-DECORATION: none } A:visited { COLOR: #996897; TEXT-DECORATION: none } A:hover { BACKGROUND-COLOR: #996897; COLOR: #610862; TEXT-DECORATION: underline } A { TEXT-DECORATION: none } .content { WIDTH: auto } .dateheader { MARGIN: 0px; PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px; POSITION: relative; WIDTH: auto; Z-INDEX: 3; min-width: 120px } .wbtn1 { BACKGROUND-COLOR: #ffffff; BORDER-BOTTOM: #996897 1px solid; BORDER-LEFT: #996897 1px solid; BORDER-RIGHT: #996897 1px solid; BORDER-TOP: #996897 1px solid; COLOR: #996897; FONT-FAMILY: verdana, arial geneva; FONT-SIZE: 7pt; TEXT-DECORATION: none } .calendar { PADDING-BOTTOM: 4px; PADDING-LEFT: 4px; PADDING-RIGHT: 4px; PADDING-TOP: 4px; TEXT-ALIGN: center } .calhead { BACKGROUND-COLOR: #ffffff; BORDER-BOTTOM: gray 1px solid; COLOR: black; TEXT-ALIGN: center; VERTICAL-ALIGN: middle; WIDTH: 14% } .calbody { BORDER-BOTTOM: 1px solid; TEXT-ALIGN: center; VERTICAL-ALIGN: middle; WIDTH: 14% } .style2 {color: #996897}
本站首页    管理页面    写新日志    退出

The Neurotic Fishbowl

[J2EE]Hibernate中outer-join与lazy关键字的使用
snowrose 发表于 2006/5/25 11:33:30

Hibernate中outer-join与lazy关键字的使用,解决大家的困惑。 <hibernate-mapping>    <class  name="com.meagle.bo.Order"    table="Orders"    dynamic-update="false"   dynamic-insert="false" >        <id  name="id"  column="Order_ID"   type="int"   unsaved-value="0">            <generator class="native">            </generator>        </id>        <set   name="orderLineItems"    table="OrderLineItem"    lazy="true"    inverse="true"   cascade="save-update"     sort="unsorted">              <key   column="Order_ID" ></key>              <one-to-many   class="com.meagle.bo.OrderLineItem" />        </set>        <property   name="userName"  type="string"  update="true"  insert="true"  access="property"            column="UserName"            not-null="true"            unique="false"  />         <property      name="total"    type="double"    update="true"   insert="true"  access="property"   column="Total"            not-null="false"            unique="false"        />    </class> </hibernate-mapping><hibernate-mapping>    <class    name="com.meagle.bo.OrderLineItem"    table="OrderLineItem"    dynamic-update="false"      dynamic-insert="false" >        <id   name="id"   column="OrderLineItem_ID"   type="int"   unsaved-value="0">            <generator class="native">            </generator>        </id>        <many-to-one   name="order"   class="com.meagle.bo.Order"   cascade="none"  outer-join="auto"   update="true"            insert="true"            access="property"            column="Order_ID" />         <property  name="description"  type="string"  update="true"  insert="true"  access="property"  column="Description"      not-null="false"   unique="false" />         <property  name="lineItemPrice"    type="double"    update="true"     insert="true"     access="property"            column="LineItemPrice"            not-null="false"            unique="false"        />    </class> </hibernate-mapping> 近来一直困惑与outer-join关键字,经过多次测试之后,现总结如下: 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 join Order p on o.OrderId=p.OrderId  where o.OrderLineItem_Id=?false:表示不使用外连接抓取关联的内容,当load(OrderLineItem.class,"id")时,Hibernate生成两条SQL语句,一条查询OrderLineItem表,另一条查询Order表。这样的好处是可以设置延迟加载,此处要将Order类设置为lazy=true。select * from OrderLineItem o where 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 specifyand then doMyObject 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 则延迟加载失效。

阅读全文(2578) | 回复(0) | 编辑 | 精华

 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)

 
 



The Neurotic Fishbowl

.: 公告




Bloginess

«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

.: 我的分类(专题)

首页(130)
RIA(22)
linux(3)
J2EE(24)
the mood of everyday(58)
oral English(14)
database(2)
soap(3)
java(9)
web developer center(14)


In the Bowl

.: 最新日志

倾情奉献iphone、ipod、ipho
check ipad's rotatio
css position fixed d
field validation whe
常见浏览器cookie个数和大小限制说明
人类无法抗拒的10种心理,学会它,就能控
读as3cookbook 有感
flex builder linux s
Component returned f
use tramp in emacs i


.: 最新回复

replcica watches
replcica watches
Dell spikes game sit
回复:人类无法抗拒的10种心理,学会它,
回复:人类无法抗拒的10种心理,学会它,
回复:flex builder linu
回复:人类无法抗拒的10种心理,学会它,
回复:人类无法抗拒的10种心理,学会它,
回复:致女人------摘自读者
回复:虚心接受别人的意见,谨慎改正


The Fishkeeper
blog名称:up forever
日志总数:130
评论数量:274
留言数量:4
访问次数:1949081
建立时间:2006年5月25日



Text Me

.: 留言板

签写新留言

参观
这个界面很漂亮!
我试试吧
up forever
该换了!


Other Fish in the Sea

.: 链接


http://blogger.org.cn/blog/more.asp?name=hongrui&id=16955

邢红瑞 

http://niegc.cublog.cn/  聂国聪

http://www.blogjava.net/SteelHand/ 铁手剑谱

http://blog.sina.com.cn/qianqq 糖qq

http://blog.csdn.net/misty_wish   师弟

http://www.bbxy.net/xiaoyu/ Goteet老师

http://www.chinaitpower.com    中国it动力

http://jinyuxi.blog.sohu.com/ 靳羽西




站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144761388 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号