-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: why shouldn't we use 'where' with 'fetch join' ?
PostPosted: Sat Apr 24, 2004 3:01 pm 
Newbie

Joined: Sat Apr 24, 2004 2:34 pm
Posts: 5
I quote Hibernate doc :
Quote:
A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause)

Nevertheless, I find the use of where+fetch really usefull for plenty of use cases !
But I may be misusing it...

My use case :
T1-->(*)T2-->(*)T3

I want to retreive one specific T3, with all its parents (All my relations must be lazy='true', and I can not use lazy fetching).

This works :
Code:
from T1 t1 left join fetch t1.t2s t2 left join fetch t2.t3s t3 where t3.name='MyT3' 


I get one T1, linked to one T2, linked to one T3 (one SQL request!)

Why should I try to avoid this way of doing ? Is there a better way to achieve the same result ?
A simple join should do it, but I won't get an object tree... And I want all objects to be linked, it is easier for the layer that uses the data.

Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 24, 2004 3:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This should rather be written:
Code:
from T3 t3 left join fetch t3.t2 t2 left join fetch t2.t1 where t3.name='MyT3'
I'd say


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 24, 2004 3:31 pm 
Newbie

Joined: Sat Apr 24, 2004 2:34 pm
Posts: 5
(Whoo ! thanks for the fast reply !)

I agree, but only for inverse=true for all collections

I really meant :
T1-->(*)T2-->(*)T3

not
T1<-->(*)T2<-->(*)T3

When you execute
Code:
from T3 t3 left join fetch t3.t2 t2 left join fetch t2.t1 where t3.name='MyT3'

you get a T3 and you can not navigate to T2

That why I wrote :
Code:
from T1 t1 left join fetch t1.t2s t2 left join fetch t2.t3s t3 where t3.name='MyT3


That's why I do not understand the documentation on this point

But I may still be missing something...
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 11:47 am 
Newbie

Joined: Sat Apr 24, 2004 2:34 pm
Posts: 5
I reply to myself, as I've done some more tests, and found that my latest reply is pure nonsens

Even with a bidirectional association I'm not able to navigate from T1 to T3, coz the framework triggers the lazy fetching for the non initialized T2 and T3

So, the conclusion is :
I must use an inverse=true relationship, and I must not use any where clause with a fetch join clause. So Micheal's answer is definitively the right to go.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 11:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
inverse="true" has absolutely nothing to do with queries.

Furthermore, as per the documentation, you can never have FETCH JOINs for two collections in the same query.

finally, if you want to apply a where condition, you must use a separate join, not a FETCH JOIN


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.