One-to-One Relationship in JPA

Deep DhamalaDeep Dhamala
6/30/2025|3 minute read
One-to-One Relationship in JPA


In this JPA (Java Persistence API) article, we will discuss different ways of achieving one-to-one mappings in JPA.


What is a One-to-One Relationship?
A one-to-one relationship is a connection between two entities where each record in one entity is related to at most one record in another entity. In our example, we will consider a student and a mark sheet, where one mark sheet can be associated with only one student and vice versa.

There are three different ways of achieving one-to-one mapping, they are:

  1. Foreign Key Method
  2. Shared Primary Key Method
  3. Join Table Method


Let’s discuss each of them below.

1. Foreign Key Method

fig: ER diagram for foreign key-based one-to-one mapping.


This method involves using a foreign key in one table that references the primary key in another table.


In our example a column marksheet_id is created in student table to reference the primary key of mark_sheet.


Below is the code associated with this case.

@Data
@Entity
public class MarkSheet {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private Integer physics;
    private Integer english;
    
    @OneToOne(mappedBy = "markSheet")
    private Student student;
}

@Getter
@Setter
@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "marksheet_id", referencedColumnName = "id")
    private MarkSheet markSheet;
}


In the above code, we are using @OneToOne annotation on the related entity field markSheet. @JoinColumn annotation configures the name of the column in the table that maps to the primary key
in the mark sheet. This is a bidirectional relationship, so we are using @OneToOne annotation on the mark sheet side too.

2. Shared Primary Key Method

Shared Primary Key method in one-to-one mapping refers to a way of establishing a relationship between two entities by having them share the same primary key.

fig: ER diagram for Shared Primary key based one-to-one mapping.

In our example, instead of creating a ID primary key column in the mark_sheet table, we are using the primary key column (student_id) which corresponds to the id field in the student table. It is illustrated below:

@Getter
@Setter
@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    
    @OneToOne(mappedBy = "student", cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private MarkSheet markSheet;
}
@Data
@Entity
public class MarkSheet {
    @Id
    @Column(name = "student_id")
    private Integer id;
    private Integer physics;
    private Integer english;
    
    @OneToOne
    @JoinColumn(name = "student_id")
    @MapsId
    private Student student;
}


Here, we have used @PrimaryKeyJoinColumn which indicates that the primary key of the student entity is used as a foreign key value for the associated mark sheet entity, which acts as a primary key for the mark sheet entity. You can see the @GeneratedValue removed because it takes its value from the student id column. @MapsId indicates that the primary key values will be copied from Student entity.

3. Join Table Method

There can be a scenario where a student may not have any mark sheet. If done with previous method, there will be null values as shown in the figure.

As there is no marksheet associated with John so it has null value in markssheet_id.

In a one-to-one mapping scenario, the join table method is an approach to establishing a relationship between two tables by introducing a third table, known as a join table or an associative table.

fig: ER diagram for Join Table based one-to-one mapping.


Generally, a join table is associated with many-to-many relationships, but using a join table in this case can help eliminate null values.

Whenever there is a relation established between student and mark sheet there is entry in student_mark_sheet table with there respective primary keys mapped.

@Data
@Entity
public class MarkSheet {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private Integer physics;

    private Integer english;

    @OneToOne(mappedBy = "markSheet")
    private Student student;
}
@Getter
@Setter
@Entity
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @OneToOne
    @JoinTable(
        name = "student_mark_sheet",
        joinColumns = @JoinColumn(name = "student_id"),
        inverseJoinColumns = @JoinColumn(name = "marksheet_id")
    )
    private MarkSheet markSheet;
}

@Jointable instructs hibernate to employ the join table strategy while maintaining the relationship.

In this article, we explored three different types of implementing a one-to-one relationship in Spring JPA.

Other resource you might find helpful:

https://www.baeldung.com/jpa-one-to-one

https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/