JPA One to One Mapping Example with Spring Boot

In this tutorial, I will explain how to define one to one relationship between two entities using JPA.

For instance we have a Person table and a License table.A Person table is associated with a single License and vice versa.Hence, Person and License table have one to one relationship with each other.

The License Account table looks like this:-

CREATE TABLE License (
expiryDate Date ,
startDate Date,
licenseId NUMBER(4) PRIMARY KEY
);

The Person table is linked with licenseId which is a foreign key in License table.

CREATE TABLE Person (
firstName varchar(50),
lastName varchar(50),
email varchar(50) ,
personId NUMBER(4) PRIMARY KEY,
licenseId NUMBER(4),
FOREIGN KEY (licenseId) REFERENCES License(licenseId)
);

Prerequsites :-

  • Spring Boot Project
  • Maven 3

To create a spring boot project you an follow this tutorial.

Now we will create the Person Entity.

Please note the annotations added for the Person POJO.We have the @JoinColumn annotation which specifies that the Person entity is linked with License entity by licenseId in database.We specifically add the name attribute to provide the foreign key (licenseId) in Person table..Then we have @OneToOne annotation which specifies that the Person and License has one to one relationship.We also specify the cascade and fetch strategy .FetchType can be Eager or Lazy. We have specified eager so that License object is also fetched with Person Object every time we make a call to fetch Person details.

By CascadeType all means all the CRUD operations performed on Person object will propagate to License object.

We will now create License entity.Note the @OneToOne annotation, it specifies the mappedBy attribute and it should refer to the license object specified in Person entity.

There are also two more annotations used here which is @JsonManagedReference and @JsonBackReference in Person and License object.I have added this to avoid the JSON infinite recursion error.If you dont add these annotations you might get the following error while fetching the person object:-

org.springframework.http.converter.HttpMessageNotWritableException”,”message”:”Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) 

Now we will create PersonRepository .It will extend the JpaRepository.

We will create PersonService and PersonServiceImpl.

Implementing the methods in the interfaces:-

Now we will create the controller:-

Run the Spring Boot application (Right click -> Run DemoApplication):-

When you start the application , it will show up in the logs like this:-

Testing the application from postman (Saving Person):-

Fetching the Person and License object which we just saved.

When you run the above postman calls you will see the insert and select statements in the console.

You will see the records created in the Person table.

Record created in License Table

That’s all on one to one mapping in Spring JPA .I hope you liked this article.

Happy Coding 🙂

Leave a comment

Your email address will not be published. Required fields are marked *