-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL: outer join cross product
PostPosted: Mon Apr 03, 2006 8:09 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
In Hibernate 2 the HQL parser allowed us to write something like:

Code:
from A a, B b where a.id=? and a.xx=b.xx(+)


using Oracles old notation for outer joins (+)

Now the new HQL parser in release 3 no longer allows that.

Is there any other way to write an outer join on two classes that have no mapped relation? The ANSI style outer join doesn't seem to work since HQL reserves that to navigate according the the mapping, e.g.

Code:
from A a left join B b on a.xx=b.xx where a.id=?


doesn't seem to work.

Also the subquery variant doesn't work:
Code:
select a,(from B b where a.xx=b.xx) from A a where a.id=?

HQL complains that inside the subquery "a" (i.e. the reference to the main query) is unknown.

I hope someone can tell me how to create an outer join "ad hoc" join/cross product. Thanks in advance,

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 7:32 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
select a, b
from A a, B b
where a.id = ? and a.xx = b.xx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 9:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The new parser is much more picky (as it should be) with what can be passed through the SQL generator. If you want outer joins you need the relationship mapped or you can use nativeSQL. In your case that should be fine since you are using an Oracle extension to the HQL anyway.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 9:35 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
For full outer, you could get clever with simple cross selects:
Code:
select a, b
from A a, B b
where (a is null or a.id = ?)
   and (b is null or a is null or a.xx = b.xx)
That, or something like it, will give you the equivalent of an full outer join. I think. Assuming that by "full outer" (which is a concept that doesn't enter into any code I've ever written) one would want all rows of both tables, joined correctly where possible, or listed with a null other-table-row where joining is not possible.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 2:32 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
tenwit wrote:
Code:
select a, b
from A a, B b
where a.id = ? and a.xx = b.xx


Well this is a normal cross product, i.e. you won't get a if there is no b for it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 04, 2006 2:40 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
david wrote:
The new parser is much more picky (as it should be) with what can be passed through the SQL generator. If you want outer joins you need the relationship mapped or you can use nativeSQL. In your case that should be fine since you are using an Oracle extension to the HQL anyway.


OK, even if it doesn't solve the problem, at least this gives me a conclusive answer that what I want is not possible in HQL at this time. Maybe it would be possible (feature request) to allow it in one form or the other, e.g. to somehow add the variant I described above:
Code:
from A a left join B b on a.xx=b.xx where a.id=?

which is a ansi-style outer join from a to b.

"for the time being" I have solved it using two queries:
Code:
from A a where a.id=?

from A a, B b where a.id=? and a.xx=b.xx


I execute the 2nd query, store the result tuples a,b in a Map from A to B.

Then I execute the first query and for each A I make a lookup in the map from A to B and either I get a B or I get null. It is slower and more cumbersome than before, but at least it still works. In our case, we retrieve 1000s of A, making a loop and executing a query for each A to see if a B exists for it would be a performance killer.

To use native sql: I'll try that too, but if possible I'd like to avoid it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.