-->
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.  [ 10 posts ] 
Author Message
 Post subject: How to write joins in HQL query as opposed to Oracle query
PostPosted: Tue Jan 09, 2007 12:05 pm 
Beginner
Beginner

Joined: Fri Dec 01, 2006 5:16 am
Posts: 23
Location: India
Can anybody please tell me the syntax for writing joins(both inner and outer) in Hibernate query language and also highlight its difference with Oracle joins? I want to join multiple tables.

Thanks & Regards,
Raul


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 12:30 pm 
Beginner
Beginner

Joined: Thu Jul 06, 2006 4:44 am
Posts: 31
You can usually create joins by using the foreign key relations or lazy collections like:
from TableA ta inner join ta.ForegnKeyToTableB tb where tb.Guid = ta.Guid and tb.Name = 'hello'

assuming you have properly defined your mappings it means that table a has foreign key to table b...If you have lazy collections defined in the class b (table b) like "TableAList" than you can write it like other way around:

from TableB tb inner join tb.TableAList ta where ta.Guid = tb.Guid

You can go further and join the third table through the table b:

from TableA ta
inner join ta.ForegnKeyToTableB tb where tb.Id = ....
inner join tb.ForeignKeyToTableC tc where tb.Guid = tc.Guid

or if only need table a and c:
from TableA ta
inner join ta.ForegnKeyToTableB.ForeignKeyToTableC tc where tc.Guid = ta.Guid

and so on...it is completely object oriented,,,

cheers
markov


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 09, 2007 3:35 pm 
Newbie

Joined: Wed Sep 06, 2006 10:35 am
Posts: 14
Is there any way to do an HQL (inner or left) join across classes that are not directly associated through the hibernate mapping file?

In SQL I can do the following inner join:

Code:
SELECT 
i.*
FROM
TABLE1 as a,
TABLE2 as b,
TABLE3 as c
WHERE
a.FOREIGNKEY_TBL2 = b.QID
AND
b.FOREIGNKEY_TBL3 = c.QID


Is there any way to do the same in HQL without specifying "<one-to-many>" or "<many-to-one>" associations in the mapping files?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 10, 2007 2:47 am 
Beginner
Beginner

Joined: Fri Dec 01, 2006 5:16 am
Posts: 23
Location: India
Hi,

Thanks for the reply.
Can you plz illustrate an outer join also.

Thankx,
Raul


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 10, 2007 9:45 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 4:44 am
Posts: 31
pastafarian wrote:
Is there any way to do an HQL (inner or left) join across classes that are not directly associated through the hibernate mapping file?

In SQL I can do the following inner join:

Code:
SELECT 
i.*
FROM
TABLE1 as a,
TABLE2 as b,
TABLE3 as c
WHERE
a.FOREIGNKEY_TBL2 = b.QID
AND
b.FOREIGNKEY_TBL3 = c.QID


Is there any way to do the same in HQL without specifying "<one-to-many>" or "<many-to-one>" associations in the mapping files?


i see no reason not to define associations if your tables are related with the foreign keys. agree?

for the outer join you may use: left join and right join (i am not sure if you need to put "fetch" at the end). the best way is to experiment with the simple tables and queries to see the results...


Top
 Profile  
 
 Post subject: HQL AND OUTER JOIN
PostPosted: Wed Jan 17, 2007 7:06 pm 
Newbie

Joined: Mon Aug 07, 2006 7:14 pm
Posts: 16
Hi all,
I have the same problem, I need to run an HQL query between two POJOs and they don't have any kind of relationship. This query is only for report purpose, so I think that I mustn't add a relationship between these classes.
In fact, the query is trying to compare values from the same POJO but in different states, i.e., two Audit class with differents dates.

I need to do something like this:


select pa2.score1 - pa.score1,
pa2.score2 - pa.score2,
pa2.score3 - pa.score3
from Audit as pa left outer join Audit as pa2
where pa.index.id=1008
and pa2.index.id=-1
and pa.page.id=pa2.page.id

In SQL is easy to do quieries like that, but in HQL, can I do this?

Anybody could help with this issue?

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 11:52 am 
Newbie

Joined: Thu May 03, 2007 6:02 am
Posts: 3
+1


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 12:18 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
My understanding is that essentially HQL is for querying objects, so unless the relationship is defined at an object (and mapping) level you can't explore the relationships between objects in HQL.

You may be able to do what you're trying to do using a subquery if your RDBMS supports it. I could be wrong of course...

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 12:26 pm 
Newbie

Joined: Mon Aug 07, 2006 7:14 pm
Posts: 16
thanks for your response, but maybe there's a misunderstanding:
[quote]unless the relationship is defined at an object (and mapping) level you can't explore the relationships between objects in HQL[/quote]
You can create a HQL query and joining two mapped objects that doesn't have any relationship between them. In these case I'm not trying to browse the association hierarchy, I just want to get the information that I need using HQL.
The problem is that I wasn't able to do an outer join between two non-associated objects, I had no problems with inner joins in HQL.

Thanks much.
Cheers.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 1:16 pm 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Apologies, I didn't read the post carefully enough. :)

Cheers,

Symon.


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