One-to-One Mapping in JPA Entities

This topic will teach us about what is one-to-one mapping in JPA entities, how to use it and different ways to do this mapping.

One-To-One_mapping_in_jpa_entities

What is one-to-one mapping in JPA?

One-to-one mapping represents that one entity is associated with another entity in JPA. In this mapping, one table establishes a relationship with another table. We have a lot of examples to demonstrate this mapping:
An employee has one address, an address is associated with a single employee instance
A student has one ID card, id card is associated with a single student instance
The one-to-one mapping has two types are:
    • One-to-one unidirectional mapping
    • One-to-one bidirectional mapping

How to use one-to-one mapping in JPA entities?

To use of the one-to-one mapping we need to create Java Bean classes and annotate with @Entity and @OneToOne JPA annotation to these classes. After the use of these annotations on Java classes map into database tables and make one-to-one relationships between these tables. If we want to use one-to-one unidirectional mapping then annotated @OneToOne annotation with one class and one-to-one bidirectional mapping then annotated @OneToOne annotation with both classes. 

What are the ways to use one-to-one mapping in JPA?

There are different ways to use this one-to-one mapping in JPA entities. We are discussing 3 important ways to use this mapping to establish a relationship between two entities. These are the following ways:
     • Using foreign key column 
     • Using shared primary key column
     • Using join table

Using foreign key column 

In this way, a foreign key column is created in the owner entity. For example, we are making a relationship between two entities(Employee and Address) in unidirectional and bidirectional then the Employee entity holds foreign of the Address entity.

Example of JPA entity one-to-one unidirectional mapping

We are creating two entities(Employee and Address) to make a one-to-one relationship between them.
Employee.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "addrs_id", referencedColumnName = "id")
private Address address;    
}

Address.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
}

→ These above two classes are mapped into database tables and made one-to-one unidirectional relationships. To know about how to implement this foreign key column one-to-one unidirectional mapping example in Spring Boot click here

Example of JPA entity one-to-one bidirectional mapping

We are creating two entities(Employee and Address) to make a one-to-one bidirectional relationship between them.
Employee.java

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "addrs_id")
private Address address;
}

Address.java

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
@OneToOne(cascade = CascadeType.ALL,mappedBy = "address")
private Employee employee;
}

→ These above two classes are mapped into database tables and made one-to-one bidirectional relationships. To know about how to implement this foreign key column one-to-one bidirectional mapping example in Spring Boot click here.

Using shared primary key column

In this way, share the primary key column between two entities. For example, we are making a relationship between two entities(Employee and Address) unidirectional and bidirectional with the shared primary key. We can use these annotations (@PrimaryKeyJoinColumn and @MapsId) to share the primary key column.

Example of JPA entity one-to-one unidirectional mapping with shared primary key

We are creating two entities(Employee and Address) to make a one-to-one relationship with @PrimaryKeyJoinColumn annotation between them.
Employee.java

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Address address;
}

Address.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
}

→ These above two classes are mapped into database tables and made one-to-one unidirectional relationships. To know how to implement this @PrimaryKeyJoinColumn annotation with a one-to-one unidirectional mapping example in Spring Boot click here

Example of JPA entity one-to-one unidirectional mapping with shared primary key

We are creating two entities(Employee and Address) to make a one-to-one bidirectional relationship with @PrimaryKeyJoinColumn annotation between them.
Employee.java

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(mappedBy="employee", cascade = CascadeType.ALL)
private Address address;
}

Address.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;
}

→ These above two classes are mapped into database tables and made one-to-one bidirectional relationships with shared primary key. To know about how to implement this one-to-one bidirectional mapping with @PrimaryKeyJoinColumn annotation in Spring Boot click here.

Example of JPA entity one-to-one unidirectional mapping with @MapsId

We are creating two entities(Employee and Address) to make a one-to-one relationship with @PrimaryKeyJoinColumn annotation between them.
Employee.java

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(cascade = CascadeType.ALL)
@MapsId
private Address address;
}

Address.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
}

→ These above two classes are mapped into database tables and made the one-to-one unidirectional relationship with MapsId. To know about how to do this one-to-one unidirectional mapping with the @MapsId annotation example in Spring Boot click here

Example of JPA entity one-to-one unidirectional mapping with @MapsId

We are creating two entities(Employee and Address) to make a one-to-one bidirectional relationship with @MapsId annotation between them.
Employee.java

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(mappedBy="employee", cascade = CascadeType.ALL)
private Address address;
}

Address.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
@OneToOne
@MapsId
private Employee employee;
}

→ These above two classes are mapped into the database tables and make the one-to-one bidirectional relationship with MapsId. To know about how to do this one-to-one bidirectional mapping with @MapsId annotation in Spring Boot click here.

Using join table

In this way, a one-to-one mapping generates a third table that holds two entities’ foreign key columns. 

Example of JPA one-to-one mapping with @JoinTable annotation

We will create two JPA entities(Employee and Address) to establish one-to-one bidirectional mapping using @JoinTable annotation to generate a third table emp_address in the database.
Employee.java

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String emailId;
private String mobNo;
private String design;
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "emp_address",
    joinColumns = {@JoinColumn(name = "emp_id", referencedColumnName = "id")},
    inverseJoinColumns = {@JoinColumn(name = "addrs_id", referencedColumnName = "id")}
)
private Address address;
}

Address.java

package com.springjava.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import lombok.Data;
@Entity
@Data
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
private String city;
private String state;
@OneToOne(mappedBy = "address")
private Employee employee;
}

→ These above two classes are mapped into the database tables and make the one-to-one bidirectional relationship with the Join Table. To know about how to implement this one-to-one bidirectional mapping with @JoinTable annotation in Spring Boot click here.

Conclusion

In this topic, we learned what is @OneToOne annotation and how to use this annotation in a JPA entity to make a one-to-one mapping in different ways(unidirectional and bidirectional).

Leave a Comment