Home One-To-One Relationship
Post
Cancel

One-To-One Relationship

One-to-One Relationship:

One of the key features of JPA is its ability to define and manage relationships between entities, including one-to-one relationships.

A one-to-one relationship in JPA refers to a relationship between two entities where each entity is related to only one instance of the other entity. For example, a relationship between a person and their passport, where each person has one passport, and each passport belongs to only one person.

One-to-One Uni-directional Relationship

A one-to-one uni-directional relationship is a type of relationship between two entities, where one entity is associated with only one instance of another entity. In this relationship, the owning entity has a foreign key reference to the other entity. The relationship is uni-directional, meaning that only one entity can access the other entity, and the other entity does not have a reference to the owning entity.

One-to-One Uni-directional Relationship Code Example

To demonstrate a one-to-one uni-directional relationship in Hibernate, we will use two entities: Person and Passport.

Person Entity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Entity
@Table(name = "person")
public class Person {

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

  private String name;

  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "passport_id", referencedColumnName = "id")
  private Passport passport;

  // Getters and Setters
}


In the Person entity, we have a one-to-one relationship with the Passport entity using the @OneToOne annotation. The cascade attribute specifies that any changes made to the Person entity should also be applied to the Passport entity. The @JoinColumn annotation specifies the name of the foreign key column in the person table.

Passport Entity:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Entity
@Table(name = "passport")
public class Passport {

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

    private String number;

    // Getters and Setters
}

In the Passport entity, we do not have any reference to the Person entity. This is because the relationship is uni-directional, and we only need to access the Passport entity from the Person entity.

Data Visualization:

Let’s take a look at how the data would be stored in the database for a one-to-one uni-directional relationship between Person and Passport entities.

Person Table:

idnamepassport_id
1John Doe1
2Jane Doe2

Passport Table:

idnumber
1ABC123
2XYZ789

In the Person table, we have a foreign key reference to the Passport table. Each person has a unique passport_id that corresponds to the id column in the Passport table. This is how we establish the one-to-one relationship between the two entities.

One-To-One Bi-directional

In a bi-directional relationship, both entities are aware of the relationship with the other entity. This means that each entity has a reference to the other entity, and both entities know about the relationship.

Person Entity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Entity
@Table(name = "person")
public class Person {

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

    private String name;

    @OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
    private Passport passport;

    // Getters and Setters
}

In the Person entity, we have a one-to-one relationship with the Passport entity using the @OneToOne annotation. The mappedBy attribute specifies the name of the field in the Passport entity that owns the relationship. We also specify the cascade attribute to ensure that any changes made to the Person entity should also be applied to the Passport entity.

Passport Entity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Entity
@Table(name = "passport")
public class Passport {

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

    private String number;

    @OneToOne
    @JoinColumn(name = "person_id")
    private Person person;

    // Getters and Setters
}

In the Passport entity, we also have a one-to-one relationship with the Person entity using the @OneToOne annotation. The @JoinColumn annotation specifies the name of the foreign key column in the passport table that references the person table.

Data Visualization

Let’s take a look at how the data would be stored in the database for a one-to-one bi-directional relationship between Person and Passport entities.

Person Table:

idname
1John Doe
2Jane Doe

Passport Table:

idnumberperson_id
1ABC1231
2XYZ7892

In the Passport table, we have a foreign key reference to the Person table, and in the Person table, we have no reference to the Passport table. This is because the relationship is bi-directional, and we only need to access the Passport entity from the Person entity or the Person entity from the Passport entity.

MappedBy

In a one-to-one relationship between two entities, there needs to be a way to specify which entity “owns” the relationship. In Hibernate, this is done using the mappedBy attribute of the @OneToOne annotation.

The mappedBy attribute is used on the inverse side of the relationship to indicate the field in the owning entity that maps to the inverse entity. This means that the entity with the mappedBy attribute does not own the relationship, but rather is just the inverse side.

In above bidirectional example we are adding mappedBy in Person entity that’s why Passport is owning the relation. based one our requirement we can use mappedBy on inverse side of the relation.

Conclusion

In conclusion, one-to-one relationships are a crucial aspect of database design and JPA provides an easy and convenient way to define and manage such relationships. Whether you are building a simple application or a complex system, understanding and using one-to-one relationships will help you to effectively model your data and ensure its integrity.

This post is licensed under CC BY 4.0 by the author.