Open In App

Hibernate – @ManyToMany Annotation

@ManyToMany annotation in Hibernate is used to obtain many-to-many relationships between two entities. It allows us to create a bidirectional relationship between two entities where each entity can be associated with another entity through multiple instances.

Examples of @ManyToMany Annotation 

Example 1:




// on the below line creating an entity for Student.
@Entity
public class Student {
    // on the below line creating an id for the section.
    @Id
      @GeneratedValue
      private long id;
    // on the below line creating a field for the student
    // name.
    private String name;
    // on the below line creating a variable for array of
    // courses and annotating it with @ManyToMany.
    @ManyToMany
      private ArrayList<Course> courses;
}
  
// on the below line creating an entity for Course.
@Entity
public class Course {
    // on the below line creating an id for course which is
    // generated value
    @Id
      @GeneratedValue
      private long id;
    // on the below line creating a field for course name.
    private String courseName;
    // on the below line creating a field for course
    // Duration.
    private int courseDuration;
    // on the below line creating a variable for array list
    // of students which are enrolled in a specific course.
    // We are annotating it with ManyToMany
    @ManyToMany 
      private ArrayList<Student> students;
}

Code Explanation:



In the above example, we are creating two entities named a Student and Course. The student entity has different fields within it such as id, name of student, and the array of courses in which the student is enrolled. The array list of students is annotated with @ManyToMany. Similarly, we are creating an entity for the Course which has several fields such as id, course name, course duration, and the array of students who have registered for this course. We are also annotating the students array list with @ManyToMany. The many-to-many annotation indicates the many-to-many relationship between the students and courses which means that one student can enroll in multiple courses and a single course will also contain a group of students registered for it.

Example 2:




// on the below line creating an entity for Author.
@Entity
public class Author {
    // on the below line creating an id for author.
    @Id 
      @GeneratedValue
      private long id;
    // on the below line creating a field for author name.
    private String name;
    // on the below line creating a variable for array of
    // books and annotating it with @ManyToMany.
    @ManyToMany
      private ArrayList<Book> books;
}
// on the below line creating an entity for Book.
@Entity
public class Book {
    // on the below line creating an id for course which is
    // generated value
    @Id 
      @GeneratedValue 
      private long id;
    // on the below line creating a field for the book name.
    private String name;
    // on the below line creating a field for the number of
    // pages for a specific book.
    private int pageCount;
    // on the below line creating a variable for an array
    // list of authors which are the author of the specific
    // book.
    @ManyToMany 
      private ArrayList<Author> authors;
}

Code Explanation:



In the above example, we are creating two entities for Book and an entity for the Author. The Book entity has several fields such as id, name, and the array list of books that are written by the current author. Similarly, we are creating an entity for Book in which we are creating different fields such as id, name, page count, and the array of authors. We are adding a @ManyToMany annotation for the array of authors which indicates that many authors can write one book as well as a book may contain multiple authors present in it. The @ManyToMany annotation indicates the many-to-many relationship between authors and books.


Article Tags :