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: inner join syntax
PostPosted: Wed Mar 22, 2006 7:44 pm 
Newbie

Joined: Wed Mar 22, 2006 7:03 pm
Posts: 3
Hello all,

I'm trying to figure out how to write an inner join in HQL. I've checked the HQL doc which doesn't seem to help .. I'm doing an inner join on the same table (from one column to a different).

The SQL that I'm trying to translate to HQL is:

SELECT * FROM pets p INNER JOIN pets pp ON
p.petid = pp.similarid and pp.name = "rex"

Any advice on how to do this with HQL?

Thanks for any advice,
-d


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 1:25 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Assuming that property SimilarPet is correctly mapped, then you'd use
Code:
select p from Pet pp
inner join pp.SimilarPet p
where pp.name = :Name
The required mapping in Pet would be something like
Code:
<many-to-one name="SimilarPet" class="Pet" column="similarid"/>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 2:11 pm 
Newbie

Joined: Wed Mar 22, 2006 7:03 pm
Posts: 3
Hmm.. Well .. the problem is that Pet doesn't have a property "SimilarPet" .. I just want to do an inner join on the same table.

A similar query is:

select * from Pet where petid in (select similarid from Pet where name = "Rex")

However this query take a very long time to complete (around 5 minutes). The "Pet" table has millions of rows.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 3:23 pm 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Try:
SELECT p FROM Pets p, Pets pp WHERE p.petId = pp.similarId AND pp.name = "rex"

I've never tried this against the same class (table) but it works for arbitrary joins against different classes (tables).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 6:50 pm 
Newbie

Joined: Wed Mar 22, 2006 7:03 pm
Posts: 3
That's it! Works great.. thanks!


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.