-->
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: How to query two columns from two different tables
PostPosted: Wed Apr 07, 2004 8:30 pm 
Newbie

Joined: Wed Apr 07, 2004 7:59 pm
Posts: 2
I'm currently evaluating Hibernate, so I'm quite new to this tool.
Could some one tell me how to do simple query like this:

select table1.attr1, table2.attr2 from table 1, table2 where ...

this kind of query is commonly used when you want to construct UI display with two or multiple tables by join them together.

I understand "join" is not a problem in Hibernate, inside the mapping file, there are classes mapped to table1 and table2, I can easily get all the attributes of table1 or table2, but how to get one attribute from table1 and one attribute from table2 like above example?

using "sub-class" concept is not appropriate for this case, since I don't want redundant data.

using "one to one" or "one to many" with collection is not appropriate also, since I have to resolve data inside the collection to my own data model (then using Hibernate become similar to use ResultSet)

any advice is highly appreciated!

Version: 2.1.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2004 8:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use theta-style join
Code:
select obj1.a, obj2.b from Obj1 obj1, Obj2 obj2 where obj1.prop1 = obj2.prop2

And read the reference guide

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2004 9:33 pm 
Newbie

Joined: Wed Apr 07, 2004 7:59 pm
Posts: 2
Thanks,
With your suggestion, by using session.createSQLQuery method, I got exact what I want!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2004 7:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can use this with HQL, too. No need to do createSQLQuery


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.