How to handle join query in Hibernate and Spring with annotations?

annotations, hibernate, java, join, spring

Solution

You are thinking in database / pure SQL terms when you talk about performing joins with select statements. The power (and danger) of Hibernate is that it abstracts this away from you and lets you think in Object terms. What you need is a relationship between the 2 objects and then let Hibernate handle this relationship.

I recommend you spend some time reading this:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html

to get a better understanding of how Hibernate can help.

Problem

I am developing application using Spring and Hibernate with MySQL. I am new to Hibernate and did basic tasks... Now I need to apply joins in select query to get data from multiple table using annotations. I have searched for it but still I didn't get any idea... Here my database tables and bean classes : ``` Table 1: 'employee_info' ( id, empid, empname, doj and jobtitle ) Table 2: 'employee_login' ( username, password, status and empid ) ``` And my bean classes are: EmployeeInfoForm.java ``` @Entity() @Table(name = "employee_info") public class EmployeeInfoForm { @Id @GeneratedValue @Column(name = "id", unique = true, nullable = true) private int id; @Column(name = "empId") private int empId; @Column(name = "empname") private String empName; @Column(name = "doj") private Date empDoj; @Column(name = "jobtitle") private String empJobTitle; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public void setEmpDoj(Date empDoj) { this.empDoj = empDoj; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public Date getEmpDoj() { return empDoj; } public void setEmp_Doj(Date empDoj) { this.empDoj = empDoj; } public String getEmpJobTitle() { return empJobTitle; } public void setEmpJobTitle(String empJobTitle) { this.empJobTitle = empJobTitle; } } ``` EmployeeLoginForm.java ``` @Entity() @Table(name = "employee_login") public class EmployeeLoginForm { @Id @Column(name = "username") private String empUserName; @Column(name = "password") private String empPassword; @Column(name = "status") private String empStatus; @Column(name = "empid") private int empId; public String getEmpUserName() { return empUserName; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public void setEmpUserName(String empUserName) { this.empUserName = empUserName; } public String getEmpPassword() { return empPassword; } public void setEmpPassword(String empPassword) { this.empPassword = empPassword; } public String getEmpStatus() { return empStatus; } public void setEmpStatus(String empStatus) { this.empStatus = empStatus; } } ``` Requirement: I want to select fields empid, empname, jobtitle from employee_info and field status from employee_login table when the empid matches on both table... Please help me to complete my work... Any suggestions and guidance are appreciated...

Original source