-->
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.  [ 7 posts ] 
Author Message
 Post subject: HQL Left Join - Please Help!!!
PostPosted: Mon Mar 05, 2012 2:17 am 
Newbie

Joined: Mon Mar 05, 2012 1:49 am
Posts: 7
Hellow guys, I've search trough the net but still I'm failed to do a left join in HQL.

So I decided to post here hoping someone might be able to help me.

Ok, I have a very simple example here.
Let's say I have two tables, student and address table. They are mapped as Student.java
and Address.java

student table:

Image



address table:


Image


Ok, in sql, to left join this two table you write:

select stu.studentid,stu.studentname, addr.studentaddress from
student stu
left join
address addr on (stu.studentid=addr.studentid);


so I will get :

result of left join
Image


Then how would I do that in hql?
I've seen different HQL codes on doing a left join, I tried them, but no luck for me.
I think I still need to do some kind of mapping relationship, but I dont know how?

I'm using JPA to map my class.

I will greatly appreciate your help!
Thank you in advance!


Top
 Profile  
 
 Post subject: Re: HQL Left Join - Please Help!!!
PostPosted: Mon Mar 05, 2012 5:22 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 3:36 pm
Posts: 78
First of all, you shall think of the subject in the OO world, that is entity objects, but not DB tables when you use Hibernate. After you have mapped those entity object properly, the HQL for your questions is something like:
Code:
select stu.studentid,stu.studentname, addr.studentaddress from Student stu left join Address addr where (stu.studentid=addr.studentid);

where Student and Address are defined entity classes.


Top
 Profile  
 
 Post subject: Re: HQL Left Join - Please Help!!!
PostPosted: Mon Mar 05, 2012 8:19 pm 
Newbie

Joined: Mon Mar 05, 2012 1:49 am
Posts: 7
Thank you for your response. I do as what you say but I've got this exception

QuerySyntaxException: Path expected for join!

I think it's because of my mapping files, but I'm not sure.
Can you check my mappings if it is correct.?

here is the code

Student.java
Code:
@Entity
@Table(name = "student")
public class Student implements java.io.Serializable {

   private String studentid;
   private String studentname;
   
         // I declared mapping relations here, is it correct?
   @OneToMany
   @JoinColumn(name="studentid")
   private Address address;
   

   public Student() {
   }

   public Student(String studentid) {
      this.studentid = studentid;
   }

   public Student(String studentid, String studentname) {
      this.studentid = studentid;
      this.studentname = studentname;
   }

   @Id
   @Column(name = "studentid", unique = true, nullable = false)
   public String getStudentid() {
      return this.studentid;
   }

   public void setStudentid(String studentid) {
      this.studentid = studentid;
   }

   @Column(name = "studentname")
   public String getStudentname() {
      return this.studentname;
   }

   public void setStudentname(String studentname) {
      this.studentname = studentname;
   }

   public void setAddress(Address address) {
      this.address = address;
   }

   public Address getAddress() {
      return address;
   }

}



Address.java
Code:
@Entity
@Table(name = "address")
public class Address implements java.io.Serializable {

   private String studentid;
   private String studentaddress;

   public Address() {
   }

   public Address(String studentid) {
      this.studentid = studentid;
   }

   public Address(String studentid, String studentaddress) {
      this.studentid = studentid;
      this.studentaddress = studentaddress;
   }

   @Id
   @Column(name = "studentid", unique = true, nullable = false)
   public String getStudentid() {
      return this.studentid;
   }

   public void setStudentid(String studentid) {
      this.studentid = studentid;
   }

   @Column(name = "studentaddress")
   public String getStudentaddress() {
      return this.studentaddress;
   }

   public void setStudentaddress(String studentaddress) {
      this.studentaddress = studentaddress;
   }

}



I also provide screenshot here:
Image

I appreciate your help and Thank you in advance!


Top
 Profile  
 
 Post subject: Re: HQL Left Join - Please Help!!!
PostPosted: Tue Mar 06, 2012 8:06 pm 
Newbie

Joined: Mon Mar 05, 2012 1:49 am
Posts: 7
Just want to up this guys.. anyhelp would be appreciated


Top
 Profile  
 
 Post subject: Re: HQL Left Join - Please Help!!!
PostPosted: Wed Mar 07, 2012 7:09 am 
Newbie

Joined: Fri Feb 17, 2012 8:20 am
Posts: 18
Yeah..
check this complete example with explanation

Hibernate Left Join, Hibernate Left Join Example

Hibernate Tutorials All Topics


Top
 Profile  
 
 Post subject: Re: HQL Left Join - Please Help!!!
PostPosted: Wed Mar 07, 2012 8:34 pm 
Newbie

Joined: Mon Mar 05, 2012 1:49 am
Posts: 7
That's Great!!! thanks for your reply! It WORKED FINALLY!...
YOU'RE A BIG HELP!!!


Top
 Profile  
 
 Post subject: Re: HQL Left Join - Please Help!!!
PostPosted: Thu Mar 08, 2012 2:35 am 
Newbie

Joined: Fri Feb 17, 2012 8:20 am
Posts: 18
You welcome friend...!!!!!!

- Java4s.com is my favorite site :-) really innovative way of presentation.


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