Open In App

Hibernate – Bidirectional Association

Last Updated : 07 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  1. One-to-One Bidirectional Association
  2. One-to-Many Bidirectional Association
  3. Many-to-One Bidirectional Association
  4. Many-to-Many Bidirectional Association

Examples of Bidirectional Association

Example 1: One-to-One Bidirectional Association

Java




package com.example.java_test_application;
  
// on below line creating an entity class for the class of
// Employee.
@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String address;
  
    @OneToOne(mappedBy = "employee")
    private IdentificationDetails identificationDetails;
  
    // on below line creating getter and setter for each of
    // them.
    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 {
  
    // on below line creating three  variables for day,
    // month and year.
    private String aadharNumber;
    private String panNumber;
    private String empID;
  
    @OneToOne
    @JoinColumn(name = "empID")
    private Employee employee;
    // on below line creating getter and setters for each of
    // them.
    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;
  
// on below line creating an entity class for the class of
// Departmet.
@Entity
public class Department {
    // on below line creating a variable for class id.
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long departmentID;
  
    // on below line creating a variable for department
    // name.
    private String departmentName;
  
    // on below line creating a variable for employee list
    @OneToMany(mappedBy = "employees")
    private List<Employee> employeeList;
  
    // on below line creating getter and setters for all
    // variables.
    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
// on below line creating a class for Employee.
public class Employee {
    // on below line creating a variable for roll number as
    // an id.
    @Id
    // on below line specifying the generated value id for
    // it.
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long empID;
  
    // on below line creating a variable for employee name
    // as name.
    private String name;
  
    // on below line creating many to one.
    @ManyToOne
    @JoinColumn(name = "departmentID")
    private Department department;
  
    // on below line creating getter and setters.
    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 {
    // on the below line creating variable for
    // departmentname and list of employees.
    private String departmentName;
    private List<Employee> employeesList;
  
    // on below line creating constructor for Department.
    public Department(String depName, List<Employee> empLst)
    {
        this.departmentName = depName;
        this.employeesList = empLst;
    }
}
public class Employee {
    // on the below line creating variables for employee
    // name, employee id and employee department.
    private String empName;
    private int empID;
    private Department department;
  
    // on below line creating a constructor for an employee
    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 {
    // on the below line creating variable for studentname
    // and list of courses in which the student is enrolled.
    private String studentName;
    private List<Course> courseList;
  
    // on below line creating constructor for Student.
    public Student(String studentName, List<Course> cList)
    {
        this.studentName = studentName;
        this.courseList = cList;
    }
}
public class Course {
    // on the below line creating variables for course name,
    // course duration and student enrolled for that course.
    private String courseName;
    private int courseDuration;
    private List<Student> studentLst;
  
    // on below line creating a constructor for the course
    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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads