[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-3875) Difference between "left join" and "left join fetch"
Sandeep Vaid (JIRA)
noreply at atlassian.com
Fri Apr 24 05:50:17 EDT 2009
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-3875?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=33006#action_33006 ]
Sandeep Vaid commented on HHH-3875:
-----------------------------------
"Left join" --->
Query q = session.createQuery("from Item i join i.bids b");
Iterator pairs = q.list().iterator();
while ( pairs.hasNext() ) {
Object[] pair = (Object[]) pairs.next();
Item item = (Item) pair[0];
Bid bid = (Bid) pair[1];
}
Instead of a List of Items, this query returns a List of Object[] arrays. At index 0 is the Item, and at index 1 is the Bid. A particular Item may appear multiple times, once for each associated Bid.
These duplicate items are duplicate in-memory references, not duplicate instances!
2. "left join fetch" ---> This is also known as eager dynamic fetching.
from Item i
left join fetch i.bids
where i.description like '%Foo%'
When executed, it returns a list of Item instances, with their bids collections fully initialized. This is quite different if you compare it to the ordered pairs returned by the queries in the previous section!
The corresponding generated SQL will be:
select i.DESCRIPTION, i.INITIAL_PRICE, ...
b.BID_ID, b.AMOUNT, b.ITEM_ID, b.CREATED_ON
from ITEM i
left outer join BID b on i.ITEM_ID = b.ITEM_ID
where i.DESCRIPTION like '%Foo%'
An additional WITH clause wouldn't make sense here. You can't restrict the Bid instances: All the collections must be fully initialized.
Personally I don't agree with author and hibernate team that WITH clause doesn't make any sense here... Why I am bound to get all the collections? What if I want only those bids whose
Amount >100 ?
> Difference between "left join" and "left join fetch"
> ----------------------------------------------------
>
> Key: HHH-3875
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-3875
> Project: Hibernate Core
> Issue Type: Task
> Reporter: Sandeep Vaid
> Priority: Minor
>
> What is the difference between the following 2 queries:
> from Product product left join product.productNamesList where product.productId = '1'
> from Product product left join fetch product.productNamesList where product.productId = '1'
>
> I know one advantage of "left join" is that i can alias the joined association and use this alias in with clause as:
> from Product product left join product.productNamesList pnList with pnList.name like "%a%" where product.productId = '1'
>
> BUT this is not possible with "left join fetch".
>
> Can we say that "left join fetch" COMPULSORY means FETCH ALL productNames for this product?
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list