One to One Bidirectional Mapping Example In Hibernate/JPA Using Spring Boot and Oracle

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

  • We have two entity Book.java and Story .java. Book and Story entity have one to one bidirectional relationship that means the Book entity has a Story entity and the Story entity also contains Book entity.
  • We will not create a table, let’s hibernate do this job.
  • After running this example we will be able to save Book as well Story in oracle database using postman.

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.

 

One to One Bidirectional Mapping In Hibernate/JPA Spring Boot

 

After running the below example we will have database entry like below.

Let’s see a complete example of One to One Bidirectional Mapping In Hibernate/JPA Spring Boot 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 – onetoonehibernatejpa, ArtifactId – onetoonehibernatejpa and name – onetoonehibernatejpa) and click on finish. Keep packaging as the jar.

Replace 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 One Bidirectional Mapping In Hibernate/JPA 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 for One to One Bidirectional In Hibernate/JPA Spring Boot example

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

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.

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 One Bidirectional In Hibernate/JPA Spring Boot

Let’s check the database. One to One Bidirectional In Hibernate/JPA Spring Boot.

One to One Bidirectional In Hibernate/JPA Spring Boot

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

 

Now test the get URL i.e http://localhost:9091/book/1

 

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

That’s all about One to One Bidirectional In Hibernate/JPA Spring Boot. If you feel any problem to run the application leave a comment.

Other association mapping example in hibernate using Spring Boot and oracle.

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

 

Inheritance Mapping in Hibernate Using Spring Boot and Oracle.

Spring Data JPA tutorials.

@OneToOne docs.

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

Top