Hibernate Join Annotation Example
Hibernate Join Annotation example:
Dowload the Required JAR files:
Hibernate Jar Files
Table Structure:
EMPNAME TABLE (empid int, empname varchar);
EMPSALARY TABLE (empid int, empsalary int); and table holds the below sample values like below,
EMPNAME: EMPSALARY
EMPID EMPNAME EMPID EMPSALARY
100 NAVEEN 100 5000
101 DIVYA 101 9000
102 HEMS 102 6000
103 RVS 103 11000
104 CHUDAR 104 7000
Project Structure:
Step 1: Create a hibernate configuration file “hibernate.cfg.xml” which has the connection url, username, password, dialect, driver and other informations.
Note: File name can be anything, but extension must be .cfg.xml
hibernate.cfg.xml:
[xml highlight=”25,26″]<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!– Database connection configurations –>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!– Making autocommit false –>
<property name="hibernate.connection.autocommit">false</property>
<!– Displaying sql queries when running the program –>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!– For JDBC connections –>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!– Mapping file –>
<mapping class="com.ngdeveloper.com.hibernate.bean.EmpName" />
<mapping class="com.ngdeveloper.com.hibernate.bean.EmpSalary" />
</session-factory>
</hibernate-configuration>[/xml]
Step 2: Create a bean (pojo) classes EmpName and EmpSalary under the package com.ngdeveloper.com.hibernate.bean to map with tables,
EmpName.java:
[java highlight=”25,26″]package com.ngdeveloper.com.hibernate.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name = "EMPNAME")
public class EmpName {
public EmpName() {
}
@Id
@GeneratedValue
@Column(name = "EMPID")
private int empId;
@Column(name = "EMPNAME")
private String empName;
@OneToOne
@PrimaryKeyJoinColumn
private EmpSalary employeeSalary;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public EmpSalary getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(EmpSalary employeeSalary) {
this.employeeSalary = employeeSalary;
}
}
[/java]
EmpSalary.java:
[java highlight=”25″]package com.ngdeveloper.com.hibernate.bean;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "EMPSALARY")
public class EmpSalary {
public EmpSalary() {
}
@Id
@GeneratedValue
@Column(name = "EMPID")
private int empId;
@Column(name = "EMPSALARY")
private int empSalary;
@OneToOne(mappedBy = "employeeSalary", cascade = CascadeType.ALL)
private EmpName employeeName;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public int getEmpSalary() {
return empSalary;
}
public void setEmpSalary(int empSalary) {
this.empSalary = empSalary;
}
public EmpName getEmployeeName() {
return employeeName;
}
public void setEmployeeName(EmpName employeeName) {
this.employeeName = employeeName;
}
}
[/java]
Step 3: Create a main (business) class EmpNameSalaryMainClass under com.ngdeveloper.com.hibernate.business and paste the below code,
[java highlight=”17″]
package com.ngdeveloper.com.hibernate.business;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import com.ngdeveloper.com.hibernate.bean.EmpName;
import com.ngdeveloper.com.hibernate.bean.EmpSalary;
public class EmpNameSalaryMainClass {
public static void main(String[] args) {
AnnotationConfiguration af = new AnnotationConfiguration();
af.configure("hibernate.cfg.xml");
SessionFactory sf = af.buildSessionFactory();
Session session = sf.openSession();
Transaction trnsact = session.beginTransaction();
List employeeNames = session.createQuery("FROM EmpName").list();
for (Iterator idNameIter = employeeNames.iterator(); idNameIter
.hasNext();) {
EmpName empNam = (EmpName) idNameIter.next();
EmpSalary empSal = empNam.getEmployeeSalary();
System.out.println("Employee Name : " + empNam.getEmpName());
System.out.println("Employee Salary : " + empSal.getEmpSalary());
}
trnsact.commit();
session.close();
}
}
[/java]
Step 4: Run the above main class.
Output:
Hibernate:
/*
FROM
EmpName */ select
empname0_.EMPID as EMPID0_,
empname0_.EMPNAME as EMPNAME0_
from
EMPNAME empname0_
Hibernate:
/* load com.ngdeveloper.com.hibernate.bean.EmpSalary */ select
empsalary0_.EMPID as EMPID1_2_,
empsalary0_.EMPSALARY as EMPSALARY1_2_,
empname1_.EMPID as EMPID0_0_,
empname1_.EMPNAME as EMPNAME0_0_,
empsalary2_.EMPID as EMPID1_1_,
empsalary2_.EMPSALARY as EMPSALARY1_1_
from
EMPSALARY empsalary0_
left outer join
EMPNAME empname1_
on empsalary0_.EMPID=empname1_.EMPID
left outer join
EMPSALARY empsalary2_
on empname1_.EMPID=empsalary2_.EMPID
where
empsalary0_.EMPID=?
Hibernate:
/* load com.ngdeveloper.com.hibernate.bean.EmpSalary */ select
empsalary0_.EMPID as EMPID1_2_,
empsalary0_.EMPSALARY as EMPSALARY1_2_,
empname1_.EMPID as EMPID0_0_,
empname1_.EMPNAME as EMPNAME0_0_,
empsalary2_.EMPID as EMPID1_1_,
empsalary2_.EMPSALARY as EMPSALARY1_1_
from
EMPSALARY empsalary0_
left outer join
EMPNAME empname1_
on empsalary0_.EMPID=empname1_.EMPID
left outer join
EMPSALARY empsalary2_
on empname1_.EMPID=empsalary2_.EMPID
where
empsalary0_.EMPID=?
Hibernate:
/* load com.ngdeveloper.com.hibernate.bean.EmpSalary */ select
empsalary0_.EMPID as EMPID1_2_,
empsalary0_.EMPSALARY as EMPSALARY1_2_,
empname1_.EMPID as EMPID0_0_,
empname1_.EMPNAME as EMPNAME0_0_,
empsalary2_.EMPID as EMPID1_1_,
empsalary2_.EMPSALARY as EMPSALARY1_1_
from
EMPSALARY empsalary0_
left outer join
EMPNAME empname1_
on empsalary0_.EMPID=empname1_.EMPID
left outer join
EMPSALARY empsalary2_
on empname1_.EMPID=empsalary2_.EMPID
where
empsalary0_.EMPID=?
Hibernate:
/* load com.ngdeveloper.com.hibernate.bean.EmpSalary */ select
empsalary0_.EMPID as EMPID1_2_,
empsalary0_.EMPSALARY as EMPSALARY1_2_,
empname1_.EMPID as EMPID0_0_,
empname1_.EMPNAME as EMPNAME0_0_,
empsalary2_.EMPID as EMPID1_1_,
empsalary2_.EMPSALARY as EMPSALARY1_1_
from
EMPSALARY empsalary0_
left outer join
EMPNAME empname1_
on empsalary0_.EMPID=empname1_.EMPID
left outer join
EMPSALARY empsalary2_
on empname1_.EMPID=empsalary2_.EMPID
where
empsalary0_.EMPID=?
Hibernate:
/* load com.ngdeveloper.com.hibernate.bean.EmpSalary */ select
empsalary0_.EMPID as EMPID1_2_,
empsalary0_.EMPSALARY as EMPSALARY1_2_,
empname1_.EMPID as EMPID0_0_,
empname1_.EMPNAME as EMPNAME0_0_,
empsalary2_.EMPID as EMPID1_1_,
empsalary2_.EMPSALARY as EMPSALARY1_1_
from
EMPSALARY empsalary0_
left outer join
EMPNAME empname1_
on empsalary0_.EMPID=empname1_.EMPID
left outer join
EMPSALARY empsalary2_
on empname1_.EMPID=empsalary2_.EMPID
where
empsalary0_.EMPID=?
[sourcecode language=”plain” light=”false” gutter=”false”]Employee Name : naveen
Employee Salary : 5000
Employee Name : divya
Employee Salary : 9000
Employee Name : hems
Employee Salary : 6000
Employee Name : rvs
Employee Salary : 11000
Employee Name : chudar
Employee Salary : 7000[/sourcecode]
Download the source code:
Recommended Hibernate Books: