Hibernate inner join using hql

hibernateinner-join

I am new to Hibernate. I have two tables, e.g., student and phone number and these two tables have a common column, student id.
I want to do an inner join with these two tables using Hibernate hql.

student.java

{
   private int id;   
   private String name;   
}

phone.java

{
   private int pid;
   private int sid;  //same id in student.java 
   private int phone_number;
}

Best Answer

Read the documentation again. You're not supposed to have the ID of the student in the Phone entity. Rather, you're supposed to have an association between both entities: a Phone belongs to a Student:

public class Phone {
    @Id
    private Integer id;

    private String phoneNumber;

    @ManyToOne
    private Student owner;
}

Only then will you be able to use joins:

// selects all the phones belonging to the students named 'John'
select phone from Phone phone where phone.owner.name = 'John'