-->
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: Joins in Hibernate
PostPosted: Sat Feb 05, 2011 1:53 am 
Newbie

Joined: Sat Feb 05, 2011 1:48 am
Posts: 2
Hi All,

In database when we joins table using like below
SELECT t1.name, t2.salary
FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;

how can we do the same thing using hibernate.

any help will be appreciated

Thanks in advance


Top
 Profile  
 
 Post subject: Re: Joins in Hibernate
PostPosted: Sat Feb 05, 2011 2:56 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
You need to define a relationship between your class Employee and your class Info.
You won't be able to join the 2 tables if they are not linked in the hibernate mapping.
For example you can define a property named info of type Info in the class Employee (one-to-one relationship).
Then you can make a request like this:

select emp.name, emp.info.salary from Employee as emp


Top
 Profile  
 
 Post subject: Re: Joins in Hibernate
PostPosted: Sun Feb 06, 2011 11:57 pm 
Newbie

Joined: Sat Feb 05, 2011 1:48 am
Posts: 2
thanks overmeulen. It worked


Top
 Profile  
 
 Post subject: Re: Joins in Hibernate
PostPosted: Mon Feb 07, 2011 1:00 pm 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
Actually, you can make (cross) joins in HQL, which are useful if you don't have explicit relationships defined:

Code:
SELECT
    emp.name,
    info.salary
FROM
    Employee emp,
    Info info
WHERE info.name = emp.name


Top
 Profile  
 
 Post subject: Re: Joins in Hibernate
PostPosted: Mon Feb 07, 2011 1:14 pm 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Indeed you can use cross joins but this is not the best choice if you take performance into account ...


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.