Many To Many Mapping In Hibernate/JPA Using Spring Boot And Oracle

In this post, we will see Many To Many Mapping In Hibernate/JPA using Spring Boot and Oracle. We will use Postman to test API. We are going to use a maven, embedded tomcat, eclipse, and oracle database.

Introduction.

Here we will have rest endpoint which will be used to save and retrieve data in the database. We have two entity Book.java and Story.java which are mapped in Many To Many relationships. In this example, we are assuming one Book can have multiple stories and one story can be associated with multiple books(Bidirectional relationships).

For this example, we are considering Book is Owner/Parent entity.

Book.java

 

Stroy.java

 

We will have three rest API as below.

http://localhost:9091/book/savebook(POST) – Save the books as well as stories.
http://localhost:9091/book/books(GET)          – Returns all books.
http://localhost:9091/story/stories(GET)        – Returns all stories.

 

Table details after localhost:9091/book/savebook operation.

We have three table book, story, and book_story. We are not going to create any table, let’s hibernate do this job.

Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle from scratch.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’click on next. Fill all details(GroupId – manytomanyhibernatejpa, ArtifactId – manytomanyhibernatejpa and name – manytomanyhibernatejpa) and click on finish. Keep packaging as the jar.

Modify the pom.xml.

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

If you see any error for oracle dependency then follow these steps. Let maven download all necessary jar. Once it is done we will able to see the maven dependency folder which contains different jar files. We can start writing our controller classes, ServiceImpl and Repository. The directory structure of the application looks as below.

Many To Many Mapping In Hibernate/JPA using Spring Boot

Book.java

Story.java

 

Define the repository interface extending CrudRepository.

BookRepository.java

StoryRepository.java

Define the Service interface.

BookService.java

Define service implementation class.

BookServiceImpl.java

Note – See here more about @Component, @Controller, @Service and @Repository annotations here.

BookController.java

StoryController.java

Note – See more details about @Controller and RestController here.

JpaConfig.java

Note – See more details about @Configuration annotations here.

Define the SpringMain.java

Note – See more details about @ComponentScan here.

And finally, we have an application.properties file where we have database details.

Let’s run the SpringMain class and test the save URL.

Testing the endpoint Using Postman.

Request data for –  http://localhost:9091/book/savebook(POST)

Response Data.

 

 

Observe the console. Below query generated by the hibernate.

Hibernate: insert into book (book_name, id) values (?, ?)
Hibernate: insert into story (story_name, id) values (?, ?)
Hibernate: insert into story (story_name, id) values (?, ?)
Hibernate: insert into book (book_name, id) values (?, ?)
Hibernate: insert into book_story (book_id, story_id) values (?, ?)
Hibernate: insert into book_story (book_id, story_id) values (?, ?)
Hibernate: insert into book_story (book_id, story_id) values (?, ?)
Hibernate: insert into book_story (book_id, story_id) values (?, ?)

 

 

Response Data for – http://localhost:9091/book/books

Response Data for – http://localhost:9091/story/stories

That’s all about Many To Many Mapping In Hibernate/JPA using Spring Boot.

Note – Default Fetch type in case of below annotations.

@OneToOne – Default fetch type is EAGER.
@OneToMany – Default fetch type is LAZY.
@ManyToOne – Default fetch type is EAGER.
@ManyToMany – Default fetch type is LAZY.

You may like.

One to One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle.
One to One Bidirectional Mapping Example In Hibernate/JPA Using Spring Boot and Oracle.
One To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.
One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
Many To One Unidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.

Inheritance Mapping in Hibernate Using Spring Boot and Oracle.

@ManyToMany docs.

 

Summary – We have seen Many To Many Mapping In Hibernate/JPA using Spring Boot.

Top