-->
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.  [ 4 posts ] 
Author Message
 Post subject: join involving two tables
PostPosted: Sat Oct 20, 2007 8:47 pm 
Beginner
Beginner

Joined: Mon Sep 03, 2007 4:25 pm
Posts: 24
I am not able to find sample hql for doing join involving two tables.

table item has a (int) userid, which refers to user table. there is a name attribute in the user table.

I want to do a sorted collection query, in the ascending order of user.name.

the equivalent sql would be
SELECT * FROM item INNER JOIN user ON item.userid=user.userid ORDER BY user.`name`

please help me with the corresponding hql.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 11:57 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
I am assuming your class structure is something like

class Item {

..
..
User user;
..
..
}


Class User {

..
..
String name;
..
..
}


Required HQL would be

select i from item i join i.user u order by u.name


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 27, 2007 9:40 pm 
Beginner
Beginner

Joined: Mon Sep 03, 2007 4:25 pm
Posts: 24
No, wrong class structure. correct one is

class Item {

int userId;
..
..
User user;

String name;
..
}

therefore, I can not do i.user, there is only i.userId, and I don't know how to do a join for that.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 7:06 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
try this

select i from Item i, User u where i.userId = u.id order by u.name

_________________
Please rate this post if you find it helpful


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