Spring Boot JPA Many to Many Relationship Mapping Example

The Spring Boot JPA Many to Many Relationship Mapping Example shows you the process of mapping an many-to-many relationship using Spring Data JPA and Spring Boot. A ManyToMany relationship in Java is where the source object has an attribute that stores a collection of target objects and (if) those target objects had the inverse relationship back to the source object it would also be a ManyToMany relationship.

Other interesting posts you may like

Let’s begin:

Project structure
Our classic mvn project in this Spring Boot JPA Many to Many Relationship Mapping Example.
Spring Boot JPA Many to Many Relationship Mapping Example

Maven dependencies
Our Spring Boot JPA Many to Many Relationship Mapping Example will use JPA, MySQL, so that we must add these dependencies in the pom.xml

Many-To-Many Relationship
We are using a database named example_manytomany and two student and subject tables . The subject and student tables have a many-to-many relationship via a join table named student_subject.
Here is the sql script

Create JPA Entities
Entity class Student and Subject are simple POJO class. Here we are using class Student and Subject with JPA @Entity annotation to map them to a database tables (these tables were created in above step).

Student entity

Subject entity

Dig deeper:

@Table annotation maps the entity with the table. If no table annotation is present, the JPA implementation will auto assign a table for the class. It means that the class name of the entity maps with the table.

@Id annotation marks the identifier property of the entity.

@Column application maps the entity’s field with the table’s column. If no column annotation is present, the JPA implementation will auto assign a column for the class. It means that the field name of the entity maps with the table’s column.

@ManyToMany annotation define a many-to-many relationship between 2 entities. All ManyToMany relationships require a JoinTable. The JoinTable is defined using the @JoinTable annotation. The JoinTable defines a foreign key to the source object’s primary key (joinColumns), and a foreign key to the target object’s primary key (inverseJoinColumns). Normally the primary key of the JoinTable is the combination of both foreign keys.

Spring Data JPA Repository
In this example, we all need to extend the JpaRepository. This is a built-in Repository implemented some common functions to work with database: findOne, findAll, save,etc.

StudentRepository

SubjectRepository

Properties Configuration

Create SpringBootApplication class

DEMO
You can go to the project directory on the console screen and run the command line

Or create the unit test class, for example

Run the above test class and see the output like below

That’s all on the Spring Boot JPA Many to Many Relationship Mapping Example.

References
Many-to-Many Relationships
Spring Boot – Database initialization

Download complete source code, click link below

spring-boot-jpa-many-to-many-mysql-example-1.zip (30 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons