Hibernate – Bidirectional Association
Last Updated :
07 Jun, 2023
Hibernate is a powerful open-source framework for Java that provides an object-relational mapping solution. It simplifies the process of working with databases which allow developers to map Java objects with the data present in the database tables. Hibernate handles the data persistence which allows developers to focus more on object-oriented aspects rather than focusing on low-level database operations.
What is Bidirectional Association in Hibernate?
In Hibernate, bidirectional association refers to the relationship between two entities where each entity has a reference to the other entity. It allows you to navigate from one entity to another entity that is associated with it and vice versa as well. We can achieve bidirectional association by defining the relationship and mapping the annotations in the entity classes. There are 4 different types of Bidirectional association which are as follows:
- One-to-One Bidirectional Association
- One-to-Many Bidirectional Association
- Many-to-One Bidirectional Association
- Many-to-Many Bidirectional Association
Examples of Bidirectional Association
Example 1: One-to-One Bidirectional Association
Java
package com.example.java_test_application;
@Entity
public class Employee {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String address;
@OneToOne (mappedBy = "employee" )
private IdentificationDetails identificationDetails;
public Long getId() { return id; }
public void setId(Long id) { this .id = id; }
public String getName() { return name; }
public void setName(String name) { this .name = name; }
public String getAddress() { return address; }
public void setAddress(String address)
{
this .address = address;
}
}
@Entity
public class IdentificationDetails {
private String aadharNumber;
private String panNumber;
private String empID;
@OneToOne
@JoinColumn (name = "empID" )
private Employee employee;
public String getAadharNumber() { return aadharNumber; }
public void setAadharNumber(String aadharNumber)
{
this .aadharNumber = aadharNumber;
}
public String getPanNumber() { return panNumber; }
public void setPanNumber(String panNumber)
{
this .panNumber = panNumber;
}
public String getEmpID() { return empID; }
public void setEmpID(String empID)
{
this .empID = empID;
}
}
|
Explanation: In the above example, we are considering an Employee having a reference to the Identification Details entity using the IdentificationDetails field. Similarly, the IdentificationDetails entity has a reference to the Employee entity using the employee field. This bidirectional association allows you to navigate from an employee to get its identification details and vice versa.
Example 2: One-to-Many Bidirectional Association
Java
package com.example.java_test_application;
import java.util.List;
@Entity
public class Department {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long departmentID;
private String departmentName;
@OneToMany (mappedBy = "employees" )
private List<Employee> employeeList;
public Long getDepartmentID() { return departmentID; }
public void setDepartmentID(Long departmentID)
{
this .departmentID = departmentID;
}
public String getDepartmentName()
{
return departmentName;
}
public void setDepartmentName(String departmentName)
{
this .departmentName = departmentName;
}
public List<Employee> getEmployeeList()
{
return employeeList;
}
public void setEmployeeList(List<Employee> employeeList)
{
this .employeeList = employeeList;
}
}
@Entity
public class Employee {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long empID;
private String name;
@ManyToOne
@JoinColumn (name = "departmentID" )
private Department department;
public Long getEmpID() { return empID; }
public void setEmpID(Long empID) { this .empID = empID; }
public String getName() { return name; }
public void setName(String name) { this .name = name; }
public Department getDepartment() { return department; }
public void setDepartment(Department department)
{
this .department = department;
}
}
|
Explanation: In the above example, each Department entity is having a collection of employees using the employee field. On the other hand, an employee entity is having a reference to the Department entity using the department field. This bidirectional association allows users to navigate from a department to its employee and from an employee to the department to which they belong.
Example 3: Many-to-One Bidirectional Association
Java
public class Department {
private String departmentName;
private List<Employee> employeesList;
public Department(String depName, List<Employee> empLst)
{
this .departmentName = depName;
this .employeesList = empLst;
}
}
public class Employee {
private String empName;
private int empID;
private Department department;
public Employee(String name, int id, Department dep)
{
this .empName = name;
this .empID = id;
this .department = dep;
}
}
|
Explanation: In the above example, we have created two entities, one for Employees and another for Department. The Employee entity is having variables for employee name, employee ID, and a department variable which tells us about the employee department in which he is working. The employee class is having a reference to the Department to which they belong. Along with that in the Department entity, we have created variables for department name and employee list. The employee list contains the list of employees who are mapped to a specific department. The association is many to one because multiple employees can belong to the same department but each employee belongs to one and only one department.
Example 4: Many-to-Many Bidirectional Association
Java
public class Student {
private String studentName;
private List<Course> courseList;
public Student(String studentName, List<Course> cList)
{
this .studentName = studentName;
this .courseList = cList;
}
}
public class Course {
private String courseName;
private int courseDuration;
private List<Student> studentLst;
public Course(String courseName, int courseDuration,
List<Student> s)
{
this .courseName = courseName;
this .courseDuration = courseDuration;
this .studentLst = s;
}
}
|
Explanation: In the above example, we are creating two entities, one for students and another for Courses. In the student entity, we are creating two variables named as student name and courseList which tells about the courses in which the student is enrolled. Along with that we have created a course entity in which we have created three variables course name, course duration, and another variable named as studentList which tells us about the list of students who are enrolled for that course. The association is many to many because a student can enroll in multiple courses whereas a course can have multiple students enrolled in it.
Share your thoughts in the comments
Please Login to comment...