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

In this tutorial, we will see One to Many Annotation Mapping Example in Hibernate/JPA using Spring Boot and Oracle. We are going to use maven, embedded tomcat, postman and oracle database. Here we will have some 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 One To Many relationships. In this example, we are assuming one Book can have multiple stories. After running the below example we will be able to save the Book and Story entity which is in One To Many relationships.

Save URL – http://localhost:9091/book/savebook

 

Get URL- http://localhost:9091/book/1

 

Database entry –

One To Many Hibernate/JPA Using Spring Boot

 

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

Before going ahead, let’s see some points.

  • We are going to use two annotations @OneToMany and @JoinColumn for mapping.
  • We will not create a table manually, let’s hibernate do this job.

Note –  Default Fetch type in case of the 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 will use the spring boot library (will provide dependency in pom.xml) to make sure all necessary jar/library is easily available for our application. Spring boot will take care of all jars. Le’s see One To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle from scratch.

Prerequisites –

  • JDK 1.8
  • Oracle 10g
  • Eclipse
  • maven
  • postman

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

Modify the pom.xml 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 are good now. We can start writing our controller classes, ServiceImpl and Repository. The directory structure of the application looks as below.

One To Many Hibernate/JPA Using Spring Boot

 

 

Define entity class i.e Book.java and Story.java.

Book.java

Story.java

 

Define the repository interface extending CrudRepository.

BookRepository.java

Define service interface i.e BookService.java

Define service implementation class.

BookServiceImpl.java

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

Define the controller class or endpoint.

BookController.java

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

Define the JpaConfig.java

Step 12 – Define the SpringMain.java

 

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

application.properties

We are almost done. Just build the project once running the main method. Open git bash or cmd and Run mvn clean install.

Let’s deploy the application running SpringMain class as a java application.

Now we will prepare json data and will try save in database.

Sample request JSON data-

 

Let’s test the save url.

One To Many Hibernate/JPA Using Spring Boot

 

Let’s check the database.

One To Many Hibernate/JPA Using Spring Boot

 

Yes, we have data in the book as well as story table. Did you notice in the book table book_id  is a foreign key?

Let’s see in the below diagram which will give us a brief about flow.

One To Many Hibernate/JPA Using Spring Boot

 

That’s all about One To Many Mapping Annotation Example In Hibernate/JPA 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 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.
Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.

@OneToMany Docs.

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

Top