0
0
Spring Bootframework~5 mins

@OneToOne relationship in Spring Boot

Choose your learning style9 modes available
Introduction

The @OneToOne relationship links two objects so each one matches exactly one object of the other. It helps keep related data connected clearly.

When a user has exactly one profile and each profile belongs to one user.
When an order has exactly one payment detail and each payment detail belongs to one order.
When a car has exactly one registration and each registration belongs to one car.
Syntax
Spring Boot
@OneToOne
@JoinColumn(name = "column_name")
private RelatedEntity relatedEntity;
Use @OneToOne on the field that holds the related object.
@JoinColumn defines the database column that links the two tables.
Examples
This links a User entity to one Profile entity using the profile_id column.
Spring Boot
@OneToOne
@JoinColumn(name = "profile_id")
private Profile profile;
This is the reverse side of the relationship, showing the Profile points back to User.
Spring Boot
@OneToOne(mappedBy = "profile")
private User user;
Sample Program

This example shows two entities, User and Profile, linked with @OneToOne. Each User has one Profile, and each Profile belongs to one User. The main method creates objects and prints the user's name and email.

Spring Boot
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import jakarta.persistence.JoinColumn;

@Entity
public class User {
    @Id
    private Long id;
    private String name;

    @OneToOne
    @JoinColumn(name = "profile_id")
    private Profile profile;

    // Constructors, getters, setters
    public User() {}
    public User(Long id, String name, Profile profile) {
        this.id = id;
        this.name = name;
        this.profile = profile;
    }
    public Long getId() { return id; }
    public String getName() { return name; }
    public Profile getProfile() { return profile; }
}

@Entity
public class Profile {
    @Id
    private Long id;
    private String email;

    @OneToOne(mappedBy = "profile")
    private User user;

    // Constructors, getters, setters
    public Profile() {}
    public Profile(Long id, String email) {
        this.id = id;
        this.email = email;
    }
    public Long getId() { return id; }
    public String getEmail() { return email; }
    public User getUser() { return user; }
}

// Example usage in a service or main method
public class Example {
    public static void main(String[] args) {
        Profile profile = new Profile(1L, "user@example.com");
        User user = new User(1L, "Alice", profile);

        System.out.println("User: " + user.getName());
        System.out.println("Email: " + user.getProfile().getEmail());
    }
}
OutputSuccess
Important Notes

Always decide which side owns the relationship; that side uses @JoinColumn.

Use mappedBy on the other side to avoid duplicate foreign keys.

Lazy loading is default; be careful when accessing related objects outside transactions.

Summary

@OneToOne links two entities with a one-to-one match.

Use @JoinColumn on the owning side to specify the database link.

mappedBy is used on the other side to complete the relationship.