From the course: Java Persistence with JPA and Hibernate

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Many-to-many relationships

Many-to-many relationships

- [Instructor] Unlike one-to-one and one-to-many relationships, it's never possible to have many-to-many relationships at database level, but this kind of relationships can exist between entity classes. For example, a user can belong to many groups and a group can have many users. To implement a many-to-many relationship, there needs to be a join table in the database, but the join table doesn't map to any entity class in ORM. It's only used behind the scenes to create the relationship. In a many-to-many relationship, any one of the entities can be the owner. Also, the relationship can be unidirectional, or bidirectional. Let's first map a unidirectional many-to-many relationship. Start by running the mariadb 06_03 sql script. In the database, there's the user and group tables. Apart from them, there's a join table, user_group, with user ID and group ID in it. I've also created two entity classes, user and group, for you. Let's start with the unidirectional relationship from the group…

Contents