One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle

In this post, we will see One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle. One book can have many stories and many stories can be associated with one book.

Before going ahead, let’s see some points.

  • We have two entity Book.java and Story.java. Book and Story entity will have One To Many and Many To One Bidirectional mapping.
  • For this example, we are assuming One book can have many stories and many stories can be associated with one book.
  • We are going to use two annotations @OneToMany and @ManyToOne annotations for mapping.
  • We will not create a table explicitly, let’s hibernate do this job.

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.

We are going to use Spring Boot, maven, embedded tomcat, postman, Eclipse and oracle database. Here we will have some rest endpoint which will be used to save and retrieve data in the database. After running the below example we will be able to save the Book and Story entity which is in @OneToMany and @ManyToOne relationship.

Rest endpoints for this example.

1. Save book(POST) – http://localhost:9091/book/savebook – Obserb in below response one book have many stories.

Request data

 

Response Data

http://localhost:9091/book/savebook

 

One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle

 

2. Get all stories(GET) – http://localhost:9091/story/stories – Get all stories details. Obserb in below response data many stories have the same book.


 

3. Get book details(GET) –  http://localhost:9091/book/{bookId}

Database details.

Let’s see One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle from scratch.

Create a new maven project and open pom.xml and replace it with the below code.

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.

One To Many Bidirectional Hibernate/Jpa Spring Boot

Book.java

Story.java

Define the repository interface extending CrudRepository.

BookRepository.java

Define the Service interface.

BookService.java

StoryRepository.java

Define service implementation class.

BookServiceImpl.java

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

BookController.java

StoryController.java

JpaConfig.java

Define the SpringMain.java

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

Let’s run the SpringMain class and test the endpoints.

http://localhost:9091/book/savebook

One To Many Bidirectional Hibernate/Jpa Spring Boot

http://localhost:9091/story/stories

http://localhost:9091/book/{bookId}

One To Many Bidirectional Hibernate/Jpa Spring Boot

That’s all about One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.

 

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.
Many To One Unidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.

 

@OneToMany Docs.

Summary – We have seen One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.

Top