JB Header
One To One Mapping in JPA with Examples
Tutorial describes One to One Mappings in JPA with examples. What are One to One Mappings One to One mappings between entities refers to those relationships where one record of an entity(say entity-1) is mapped exactly to one record of another entity(say entity-2). Then entity-1 and entity-2 are said to be in a One to One relationship with each other. Or, it can be said that a One to One mapping exists between the two entities. Example Database Table Structure with a One to One Mapping An apt scenario for this would be the relation between an employee of a company and the car parking slot that he is assigned to park his vehicle. Both have a one-to-one mapping with each other.

Lets say we have two entities Employee and ParkingSlot represented by equivalent tables EMPLOYEE and PARKING_SLOT in the database schema.

The table structures for EMPLOYEE and PARKING_SLOT tables are as below:
EMPLOYEE and PARKING_SLOT tables
-- EMPLOYEE TABLE--
EMPLOYEE_ID INT(10)(P.K);
PARKING_SLOT_ID INT(10)(F.K);
EMP_NAME VARCHAR(50);
EMP_ADDRESS VARCHAR(100);
-- rest of the table definition goes here
-- PARKING_SLOT TABLE -- PARKING_SLOT_ID INT(10)(P.K); PARKING_SLOT_LOCATION VARCHAR(50); -- rest of the table definition goes here
The column PARKING_SLOT_ID in EMPLOYEE table is a foreign key to PARKING_SLOT_ID column in PARKING_SLOT table. This looks very similar to the many-to-one mapping between tables. To make it one-to-one we need to add a unique constraint on the PARKING_SLOT_ID column in the EMPLOYEE table. How JPA takes care of one-to-one mappings For explaining the JPA way of implementing One to One Mappings I will use the EMPLOYEE and PARKING SLOT relation defined above.

The annotation defined by JPA for one-to-one mappings is ‘@OneToOne’. In the table structure above the PARKING_SLOT_ID foreign key is in EMPLOYEE table. This makes the EMPLOYEE table as the owner of the relationship and hence the @JoinColumn definition will need to be placed in the employee table along with the @OnetoOne annotation as shown below in the POJO definition for Employee entity -
POJO definition for employee entity - Employee.java
//Employee.java
 @Entity
 public class Employee {
  @Id private int employeeId;
  private String name;
  
  @OneToOne
  @JoinColumn(name="PARKING_SLOT_ID")
  private ParkingSlot parkingSlot;

  //Getters/Setters and rest of the code
}
Explanation of the code
In the above code OneToOne mapping has been defined from the Employee entity to the ParkingSlot entity using the @OneToOne annotation and the @JoinColumn annotation defines the join column i.e. the column which stores the foriegn key to the PARKING_SLOT table as PARKING_SLOT_ID.
Definition for parking slot POJO entity The definition for parking slot POJO entity is as follows -
ParkingSlot.java
//ParkingSlot.java
 @Entity
 public class ParkingSlot{
  @Id private int parkingSlotId;
  private String parkingSlotLocation;

 //Getters/Setters and the rest of the code
}
Bi-Directional One-To-One mappings The one-to-one mapping defined above is actually unidirectional with EMPLOYEE table as the owner of the relationship. Now suppose we want the parking slot also to maintain the reference to the employee that is using the parking slot in order to make the relationship bidirectional.I.e. we need to add a foreign key to EMPLOYEE table in the PARKING_SLOT table. The PARKING_SLOT table would then look like -
PARKING_SLOT TABLE for Bi-directional relationship
-- PARKING_SLOT TABLE --
PARKING_SLOT_ID  INT(10)(P.K);
PARKING_SLOT_LOCATION VARCHAR(50);
EMPLOYEE_ID INT(10)(F.K);
-- rest of the table definition goes here
The ParkingSlot POJO entity with the newly added reference to Employee entity
ParkingSlot POJO entity with reference to Employee entity
//ParkingSlot.java
 @Entity
 public class ParkingSlot{
  @Id private int parkingSlotId;
  private String parkingSlotLocation;
  
  @OneToOne
  @JoinColumn(name="PARKING_SLOT_ID")
  private ParkingSlot parkingSlot;
  
  //Getters/Setters and the rest of the code
}
The employee entity would remain the same as defined previously as it already had a @OneToOne foreign key defined to ParkingSlot table. Summary In this tutorial we looked at what are One to One Mappings(or relationships), an example database table structure with One to One relationships between its tables, how JPA will implement this One to One Mapping in terms of Java POJOs and finally we looked at how to make the JPA implementation bi-directional. This concludes the tutorial on One to One Mappings in JPA.