-->
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.  [ 8 posts ] 
Author Message
 Post subject: problem with multiple table join
PostPosted: Mon Oct 06, 2003 7:02 pm 
Newbie

Joined: Tue Sep 09, 2003 3:40 pm
Posts: 9
Hi,

I cannot find any example from doc, so I am asking this here. This may be
very simple for you guys, please bear with me.

I try to do a join based on multiple tables, for example: if I have
a Book table, and a User table, a Book can have multiple users, and they have one-to-many bidirectional association. That mean User table can have a FK which is identical to PK of Book. In SQL, I can do something like the following to retrieve all user names of one book:

select user.name from Book book, User user where user.book_id = book.book_id;

How can we do the above in HQL? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Assuming you have this "one-to-many bidirectional association" mapped something like:
Code:
<class name="User" table="user"...>
    <id...>
    <property name="name" column="name"/>
    <many-to-one
            name="book"
            class="Book"
            ...
    />
    ...
</class>


Then the HQL would be:
select u.name from User as u where u.book.id=:bookId
to retreive just the user's name attribute, or:
from User as u where u.book.id=:bookId
to retreive the entire User instance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:18 pm 
Newbie

Joined: Tue Sep 09, 2003 3:40 pm
Posts: 9
I think I may have figure it out as following:

select user.name from User user where user.book.boo_id = ?

It means to retrieve all user names based on a given book id,
is this correct, need you guys confirmation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I am assuming that "boo_id" is the column name? Don't do that. HQL deals with your java object model, not the relational model.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:22 pm 
Newbie

Joined: Tue Sep 09, 2003 3:40 pm
Posts: 9
Thanks, Steve. Your explanations make my probelm go away.

steve wrote:
Assuming you have this "one-to-many bidirectional association" mapped something like:
Code:
<class name="User" table="user"...>
    <id...>
    <property name="name" column="name"/>
    <many-to-one
            name="book"
            class="Book"
            ...
    />
    ...
</class>


Then the HQL would be:
select u.name from User as u where u.book.id=:bookId
to retreive just the user's name attribute, or:
from User as u where u.book.id=:bookId
to retreive the entire User instance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:26 pm 
Newbie

Joined: Tue Sep 09, 2003 3:40 pm
Posts: 9
steve wrote:
I am assuming that "boo_id" is the column name? Don't do that. HQL deals with your java object model, not the relational model.


You are right, Steve. That should be Id attribute of Book object, not the column name. I guess :bookId in your query example is assuming Book has a Id attribute, is that correct?

It seems Hibernate is really powerful!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 7:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
:bookId is what is called a "named parameter". Much nicer than the '?' version. Hibernate also supports the '?' form if you feel more comfortable with that.

So either:
Code:
Long bookIdToRetreive = ...;
Query query = session.createQuery("select u.name from User as u where u.book.id=:bookId");
query.setLong("bookId", bookIdToRetreive.longValue());
query.list()...


or
Code:
Long bookIdToRetreive = ...;
Query query = session.createQuery("select u.name from User as u where u.book.id=?");
query.setLong(1, bookIdToRetreive.longValue());
query.list()...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 06, 2003 11:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Steve illustrates a valid reason to use his first example rather than the second as the index starts at 0 rather than 1. This issue is avoid by using named parameters.


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