One to One Mapping in Hibernate/JPA using Spring Boot and Oracle

In this post, we will see One to One Mapping Example in Hibernate/JPA using Spring Boot and Oracle.

We are going to use a 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. After running the below example we will be able to save the Book and Story entity which is in one to one relationships.

One to One Mapping in Hibernate/JPA using Spring Boot and Oracle

 

Before going ahead, let’s see some points.

  • We have two entities Book.java and Story .java which are in one to one relation.
  • For this example, we are assuming one book can have only one story. The below example is a unidirectional relationship that means the Book entity can have a Story entity but the Story entity will not have a Book entity.
  • We are going to use two annotations @OneToOne and @JoinColumn for mapping.
  • We are not going to create a table, let’s hibernate do this job.
  • In case of @OneToOne default fetch type is EAGER.

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.

Let’s see One to One Mapping in Hibernate/JPA using Spring Boot and Oracle.

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. Let’ s start.

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 – onetoonehibernatejpa, ArtifactId – onetoonehibernatejpa and name – onetoonehibernatejpa) 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.

Step 5 – Let maven download all necessary jar. Once it is done we will able to see the maven dependency folder which contains different jar files.

One to One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle

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 One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle

 

Step 6 – Define entity class i.e Book.java and Story.java.

Book.java

Story.java

 

Step 7 – Define the repository interface extending CrudRepository.

BookRepository.java

Step 8 – Define service interface i.e BookService.java

Step 9 – Define service implementation class.

BookServiceImpl.java

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

Step 10 – Define the controller class or endpoint.

BookController.java

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

Step 11 – Define the JpaConfig.java

Step 12 – Define the SpringMain.java

Note – See more details about @ComponentScan here.

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 prepare the json data and try to save in the database.

Sample request JSON data-

 

Let’s test the save url.

One to One Mapping in Hibernate/JPA using Spring Boot and Oracle

 

Let’s check the database.

One to One Mapping Spring Boot

Yes, we have record in the book as well as story table. Did you notice storyiwd is the column in the book table is a foreign key? This is the primary key for the story table.

Let’s see in the below diagram which will give us a brief about flow.One to One Mapping in Hibernate/JPA using Spring Boot and Oracle

That’s all about One to One Mapping in Hibernate/JPA using Spring Boot and Oracle. If you feel any problem to run the application leave a comment.

You may like.

@OneToOne docs.

Summary – We have seen One to One Mapping in Hibernate/JPA using Spring Boot and Oracle. In one to one mapping one entity associated with another entity.

 

Top