Hibernate - Fetching associations

刚又去看了看Hibernate in action,fetching associations

In HQL, you can specify that an association should be eagerly fetched by an
outer join using the fetch keyword in the from clause:

from Item item
left join fetch item.bids
where item.description like '%gc%'
 


This query returns all items with a description that contains the string gc, and all
their bids, in a single select. When executed, it returns a list of Item instances, with
their bids collections fully initialized. We call this a from clause fetch join. The purpose
of a fetch join is performance optimization: We use this syntax only because
we want eager initialization of the bids collections in a single SQL select

 

 

■ HQL always ignores the mapping document eager fetch (outer join) setting. If
you’ve mapped some associations to be fetched by outer join (by setting
outer-join="true" on the association mapping), any HQL query will ignore
this preference. You must use an explicit fetch join if you want eager fetching
in HQL.

 

原来在映射文件里设置了outer-join="ture",如果在HQL不用fetch的话,一样不会initialized.

希望大家也注意一下:)

BTW:join fetch很好用,前面不知道,每次在jsp因为没有initialized关闭了session而头痛:)

友财网作者 http://www.ucai8.com

你可能感兴趣的:(sql,jsp,Hibernate,performance)