Hibernate - @ManyToMany Annotation Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report @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: Java // 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: Java // 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. Comment More infoAdvertise with us Next Article Java Tutorial C chaitanyamunje Follow Improve Article Tags : Java Java-Hibernate Java-Spring-Data-JPA Hibernate- Annotations Practice Tags : Java Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Arrays in Java In Java, an array is an important linear data structure that allows us to store multiple values of the same type. Arrays in Java are objects, like all other objects in Java, arrays implicitly inherit from the java.lang.Object class. This allows you to invoke methods defined in Object (such as toStri 9 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 9 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read Like