Spring Boot JPA One to One
Lets learn how to use Spring Boot JPA to implement One to One relationship.
You can model One-to-One relationships between entities using the @OneToOne annotation. In a one-to-one relationship, each record in one table is associated with one and only one record in another table.
Bidirectional One-to-One Relationship
We can have two entities Person and Address. A person can have only one address and an address belongs to only one person.
Person entity:
1 |
|
The @OneToOne annotation is used to create a one-to-one relationship between the Person and Address entities. The mappedBy attribute is used to specify the property in the Address entity that owns the relationship.
Address entity:
1 |
|
An address belongs to only one person. The @JoinColumn annotation is used to specify the foreign key column in the Address entity that refers to the primary key column in the Person entity.
Test the relationship by creating a Person and an Address and setting the address to the person.
1 | Person person = new Person(); |
Unidirectional One-to-One Relationship
It is also possible to create a unidirectional one-to-one relationship.
Person entity:
1 |
|
Address entity:
1 |
|
Test:
1 | Person person = new Person(); |
@MapsId Example
In some cases, you may want the primary key of the child entity to be the same as the primary key of the parent entity. You can achieve this using the @MapsId annotation. @MapsId can be used in a One-to-One relationship or a Many-to-One relationship.
1 |
|
1 |
|