Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Hibernate - Many to Many using Annotations

Just like 'One to One' and 'One to Many', Many to Many can also be achieved using annotations in an easier way.

An 'Employee' can be assigned to many 'Project'. At the same time a 'Project' can also have multiple 'Employee' objects or employees.

Let us see the 'Employee' class:


class Employee {

  @Id
  @Column(name="ID")
  String id;

  @Column(name="NAME")
  String name;

  @ManyToMany(mappedBy="employee", Cascade=CascadeType.ALL)
  Set <Project> projectSet = new HashSet <Project>();

  ---Getters & Setters---
}


An 'Employee' can be assigned to various 'Project'. So, in the 'Employee' class we have put:

Set <Project> projectSet = new HashSet <Project>();

And just above it we have put the @ManyToMany annotation.

@ManyToMany(mappedBy="employeeSet", Cascade=CascadeType.ALL)

mappedBy="employeeSet" tells Hibernate that Many to Many mapping has to be done with the 'employeeSet' object of the 'Project' class.

Cascade=CascadeType.ALL - If the Employee object is saved, updated or deleted, the Project objects associated to it should also be saved, updated or deleted.

The 'Project' class is defined below.


class Project{

   @Id
   @Column(name="PROJECT_ID")
   int projectId;

   @Column(name="PROJECT_NAME")
   String projectName;

   @ManyToMany
   Set <Employee> employeeSet = new HashSet <Employee>();

   ---Getters & Setters---
}

Since, we are establishing a bi-directional relationship, we have put @ManyToMany annotation on top of

Set <Employee> employeeSet = new HashSet <Employee>();

So, we will be writing the main class where we will be saving the 'Employee' data and its corresponding 'Project' data to the database using Hibernate.


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSave {
   public static void main(String[] args) {

    static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Set <Project> projectSet = new Set <Project>();

    Project projectPhills = new Project();
    projectPhills.setProjectId("A001");
    projectPhills.setProjectName("Phills Project");

    projectSet.add(projectPhills);

    Project projectONeal = new Project();
    projectONeal.setProjectId("B001");
    projectONeal.setProjectName("O Neals Project");

    projectSet.add(projectONeal);

    Employee employee = new Employee();
    employee.setId(1);
    employee.setName("Joe");
    employee.setProjectSet(projectSet);
    session.save(employee);

    session.getTransaction().commit();

    session.close();
    sessionFactory.close();
  }
}

EMPLOYEE

ID NAME
1 Joe

PROJECT

PROJECT_ID PROJECT_NAME
A001 Phills Project
B001 O Neals Project

EMPLOYEE_PROJECT

ID PROJECT_ID
1 A001
1 B001

Note : If mappedBy="employeeSet" was not given with @ManyToMany annotation in the Employee class, Hibernate would have created an extra table 'PROJECT_EMPLOYEE'. Which we don't want.