The @ManyToMany annotation maps a many-to-many relationship between two JPA entities, where each row on one side can relate to many rows on the other and the reverse. Because relational databases cannot store this directly, JPA persists it in a separate join table that holds one foreign key to each entity. One side of the relationship owns that table (it declares @JoinTable) and the other side points to it with mappedBy.
A student enrolling in many courses, and a course having many students, is the standard example. The code below uses the jakarta.persistence package of Jakarta Persistence 3.x (Spring Boot 3 and Hibernate 6). On older stacks the package is javax.persistence and everything else is identical.
For this model:
@ManyToMany goes on a collection-valued field. JPA creates and maintains the student_course table; you do not write an entity for it. If you omit @JoinTable, the provider generates default table and column names, which are rarely what you want, so define them explicitly.
In a bidirectional relationship both entities have a collection. Exactly one side is the owner. Only changes made on the owning side are written to the join table, and the inverse side uses mappedBy with the name of the owner's field.
Here Course is the owner: joinColumns is the foreign key that refers to the owner (course_id), and inverseJoinColumns refers to the other entity (student_id). The addStudent and removeStudent helpers update both in-memory sides so the object graph stays consistent, even though only the owner's collection drives the SQL.
A common mistake is to add a course to student.getCourses() only. Because Student is the inverse side, Hibernate ignores that change and nothing is stored.
If only one entity needs to navigate the relationship, drop the collection on the other side. The entity holding the collection is automatically the owner and declares @JoinTable:
Course then has no reference to Student and needs no annotation. Choose unidirectional when you never need to ask a course for its students; a repository query can still answer that question.
After enrolling students 1 and 2 in course 101 and student 1 in course 102, the join table contains:
| student_id | course_id |
|---|---|
| 1 | 101 |
| 2 | 101 |
| 1 | 102 |
These defaults and choices cause most production problems with many-to-many mappings.
@ManyToMany is lazy by default. Keep it lazy and load what you need with a JOIN FETCH query or an entity graph to avoid N+1 selects.CascadeType.PERSIST and MERGE are reasonable. Do not use CascadeType.REMOVE or ALL; deleting one student would also delete shared courses that other students still use.Set over List. With a List (a bag in Hibernate terms), removing one element makes Hibernate delete all join rows for that owner and insert the remaining ones again.equals and hashCode for entities stored in a Set, based on a business key or a stable id, not on mutable fields.If the relationship has its own data, such as an enrollment date or a grade, @ManyToMany is no longer enough because the join table cannot have attributes. Model the join table as an entity and use two @ManyToOne associations:
Student and Course then each hold a @OneToMany(mappedBy = "student") or mappedBy = "course" collection of Enrollment.
Either works. Pick the side that is naturally updated, such as Course adding students, and keep the other side as mappedBy. The choice affects only which collection writes the join table.
You most likely modified the inverse side (the one with mappedBy). Update the owning collection, ideally through helper methods that change both sides.
Use Set for most mappings. It avoids the delete-and-reinsert behavior Hibernate shows for bags and matches the real semantics, since a student cannot be enrolled in the same course twice.
Not with @ManyToMany. Introduce an association entity such as Enrollment and map two @ManyToOne relationships.
@ManyToMany maps a many-to-many relationship through a join table defined by @JoinTable on the owning side, with mappedBy on the inverse side. Use Set, keep both sides in sync with helper methods, stay lazy, avoid REMOVE cascades, and switch to an association entity when the relationship carries its own data.