浏览 7364 次
锁定老帖子 主题:eager-fetch如何才能生效?
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-04-20
关键配置: <set name="cards" inverse="true" cascade="all,delete-orphan"> <key column="CUSTOMER_ID" foreign-key="fk_card_customer"/> <one-to-many class="Card" /> </set> <many-to-one name="customer" class="Customer" column="CUSTOMER_ID" fetch="join" lazy="false"/> hibernate.max_fetch_depth=100 hibernate.default_batch_fetch_size=10 结果是fetch="join"不起作用,执行了如下1+N条sql: Hibernate: select * from ( select ... from CARD card0_ where 1=1 and card0_.AMOUNT_Y>=? ) where rownum <= ? Hibernate: select ... from CUSTOMER customer0_ where customer0_.ID=? Hibernate: select ... from CUSTOMER customer0_ where customer0_.ID=? Hibernate: select ... from CUSTOMER customer0_ where customer0_.ID=? Hibernate: select ... from CUSTOMER customer0_ where customer0_.ID=? Hibernate: select ... from CUSTOMER customer0_ where customer0_.ID=? Hibernate: select ... from CUSTOMER customer0_ where customer0_.ID=? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-04-20
用的HQL 还是 Criteria ?
用Criteria马上生效 |
|
返回顶楼 | |
发表时间:2007-04-20
hql
发现必须显示的写from Card c join c.customer 才行。 |
|
返回顶楼 | |
发表时间:2007-04-20
引用 The fetch strategy defined in the mapping document affects:
* retrieval via get() or load() * retrieval that happens implicitly when an association is navigated * Criteria queries * HQL queries if subselect fetching is used Usually, we don't use the mapping document to customize fetching. Instead, we keep the default behavior, and override it for a particular transaction, using left join fetch in HQL. This tells Hibernate to fetch the association eagerly in the first select, using an outer join. In the Criteria query API, you would use setFetchMode(FetchMode.JOIN). reference上的说明 |
|
返回顶楼 | |
发表时间:2007-04-20
如果是这样
many-to-one端的fetch="join" 只有在get,load时候才行,这时候去join根本就是副作用,因为get,load查询一条记录,many-to-one也肯定只有一条记录,sql就是1+1 ,分开查询的效率将更高。 retrieval that happens implicitly when an association is navigated --这句话怎么理解? |
|
返回顶楼 | |
发表时间:2007-04-20
eyejava 写道 如果是这样
many-to-one端的fetch="join" 只有在get,load时候才行,这时候去join根本就是副作用,因为get,load查询一条记录,many-to-one也肯定只有一条记录,sql就是1+1 ,分开查询的效率将更高。 前提:eager-fetch,使用get 或 load.默认抓取,2条。join.1条 除非是用lazy,用batch initial...,那么通过batch ini..可以一次抓取多个关联对象,语句可能减少。 引用 retrieval that happens implicitly when an association is navigated --这句话怎么理解?
比如访问一个lazy对象,这个的many-to-one是join |
|
返回顶楼 | |
发表时间:2007-07-27
eyejava 写道 hql
发现必须显示的写from Card c join c.customer 才行。 不对吧,hql中的eager-fetch不是这样写的。 请使用 left join fetch |
|
返回顶楼 | |