使用hql进行Hibernate内连接

Liv*_* As 5 hibernate inner-join

我是Hibernate的新手.我有两个表,例如,studentphone number这两个表有一个共同的列,student id.我想使用Hibernate hql对这两个表进行内连接.

student.java

{
   private int id;   
   private String name;   
}
Run Code Online (Sandbox Code Playgroud)

phone.java

{
   private int pid;
   private int sid;  //same id in student.java 
   private int phone_number;
}
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 6

再次阅读文档.您不应该在Phone实体中拥有学生的ID.相反,你应该在两个实体之间建立关联:手机属于学生:

public class Phone {
    @Id
    private Integer id;

    private String phoneNumber;

    @ManyToOne
    private Student owner;
}
Run Code Online (Sandbox Code Playgroud)

只有这样你才能使用连接:

// selects all the phones belonging to the students named 'John'
select phone from Phone phone where phone.owner.name = 'John'
Run Code Online (Sandbox Code Playgroud)