A Many-to-Many relationship exists when multiple records in one table are associated with multiple records in another table.
For example:
✅ This requires a Join Table to link the two entities.
Student Entity (Many-to-Many with Course)Course Entity (Many-to-Many with Student)student_course) for mapping.@ManyToMany → A student can enroll in many courses.@JoinTable → Defines the join table person_course.course.getPersons().add(this);.mappedBy = "courses" → This refers to the courses field in the Student entity.Create a separate Hibernate utility class to manage SessionFactory:
student_course table.person_course table.Table name : person
| id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
Table name : course
| id | title |
|---|---|
| 1 | Java Programming |
| 2 | Python Programming |
Join Table: student_course
| student_id | course_id |
|---|---|
| 1 | 2 |
| 1 | 2 |
| 2 | 1 |