Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Hibernate - @ManyToMany Annotation
Next article icon

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.


Next Article
Hibernate - @ManyToMany Annotation

C

chaitanyamunje
Improve
Article Tags :
  • Java
  • Java-Hibernate
  • Java-Spring-Data-JPA
  • Hibernate- Annotations
Practice Tags :
  • Java

Similar Reads

    Hibernate - @OneToMany Annotation
    @OneToMany annotation in Hibernate is used to obtain one-to-many relationships between two entities. It is used to map a collection-valued association where a single instance of an entity is mapped to multiple instances of another entity. Examples of @OneToMany AnnotationExample 1: Java // on the be
    4 min read
    Hibernate - @ManyToOne Annotation
    @ManytoOne annotation in Hibernate is used to create a many-to-one relationship between two entities. The @ManyToOne annotation indicates that the many instances of one entity can be associated with only one instance of another entity. When we annotate a field of the method with @ManyToOne annotatio
    4 min read
    Hibernate - @OneToOne Annotation
    @OnetoOne annotation in Hibernate is used to create a one-to-one association between two entities. The one-to-one annotation indicates that one instance of an entity is associated with only one instance of the other entity. When we annotate a field or method with @Onetoone annotation then Hibernate
    4 min read
    Hibernate - @Lob Annotation
    Many times there is a scenario while storing the data in the database that we have to store images or files within our database for a specific entity. In that case, we can use @Lob annotation which will help us to map large binary objects or large character objects to a specific entity in our databa
    3 min read
    Hibernate - @MapsId Annotation
    @MapsId annotation in Hibernate is used to obtain a one-to-one relationship between two entities by mapping the primary key of one entity to the foreign key of another entity.  This annotation is used when we have to use a shared primary key between two entities.  Examples for @MapsId AnnotationExam
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences