The @OneToOne relationship links two objects so each one matches exactly one object of the other. It helps keep related data connected clearly.
@OneToOne relationship in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Spring Boot
@OneToOne
@JoinColumn(name = "profile_id")
private Profile profile;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()); } }
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.
Practice
1. What does the
@OneToOne annotation represent in Spring Boot JPA?easy
Solution
Step 1: Understand the meaning of @OneToOne
The @OneToOne annotation defines a direct one-to-one link between two entities in JPA.Step 2: Compare with other relationship types
Unlike @OneToMany or @ManyToMany, @OneToOne means exactly one entity matches exactly one other entity.Final Answer:
A relationship where one entity is linked to exactly one other entity -> Option AQuick Check:
@OneToOne = one-to-one link [OK]
Hint: One entity matches exactly one other entity [OK]
Common Mistakes:
- Confusing @OneToOne with @OneToMany
- Thinking it allows multiple linked entities
- Ignoring the uniqueness of the relationship
2. Which annotation is used on the owning side of a
@OneToOne relationship to specify the foreign key column?easy
Solution
Step 1: Identify the owning side annotation
The owning side uses @JoinColumn to specify the foreign key column in the database.Step 2: Differentiate from mappedBy
@MappedBy is used on the inverse side, not the owning side.Final Answer:
@JoinColumn -> Option CQuick Check:
Owning side uses @JoinColumn [OK]
Hint: Owning side uses @JoinColumn for foreign key [OK]
Common Mistakes:
- Using @MappedBy on owning side
- Confusing @Column with @JoinColumn
- Forgetting to specify @JoinColumn
3. Given the following code snippet, what will be the output when fetching
Person and accessing person.getPassport().getNumber()?
@Entity
class Person {
@Id
private Long id;
@OneToOne
@JoinColumn(name = "passport_id")
private Passport passport;
// getters and setters
}
@Entity
class Passport {
@Id
private Long id;
private String number;
// getters and setters
}medium
Solution
Step 1: Analyze the @OneToOne mapping
The Person entity owns the relationship with @JoinColumn, so passport is linked properly.Step 2: Understand the data fetching
When fetching Person, accessing person.getPassport().getNumber() returns the linked Passport's number if data exists.Final Answer:
Returns the passport number linked to the person -> Option BQuick Check:
Proper @OneToOne with @JoinColumn returns linked entity [OK]
Hint: Owning side with @JoinColumn returns linked entity [OK]
Common Mistakes:
- Assuming mappedBy is required on owning side
- Expecting NullPointerException without data check
- Confusing compilation errors with runtime behavior
4. Identify the error in this
@OneToOne mapping:
@Entity
class User {
@Id
private Long id;
@OneToOne(mappedBy = "user")
private Profile profile;
}
@Entity
class Profile {
@Id
private Long id;
@OneToOne
private User user;
}medium
Solution
Step 1: Check owning side annotations
Profile is the owning side but lacks @JoinColumn to specify the foreign key.Step 2: Understand mappedBy usage
mappedBy is correctly on User side, indicating inverse side.Final Answer:
Missing @JoinColumn on Profile entity owning side -> Option AQuick Check:
Owning side needs @JoinColumn [OK]
Hint: Owning side must have @JoinColumn [OK]
Common Mistakes:
- Placing mappedBy on owning side
- Omitting @JoinColumn on owning side
- Confusing owning and inverse sides
5. You want to create a bidirectional
@OneToOne relationship between Employee and Desk. Which is the correct way to define the relationship so that Employee owns the relationship and the foreign key is in the Desk table?hard
Solution
Step 1: Determine owning side and foreign key location
The foreign key is in Desk table, so Desk owns the relationship.Step 2: Correct annotation placement
Desk must have @JoinColumn and no mappedBy; Employee uses mappedBy to point to Desk's field.Step 3: Match option with correct annotations
In Employee: @OneToOne(mappedBy = "employee") Desk desk; In Desk: @OneToOne @JoinColumn Employee employee; matches this.Final Answer:
In Employee: @OneToOne(mappedBy = "employee") Desk desk; In Desk: @OneToOne @JoinColumn Employee employee; -> Option DQuick Check:
Foreign key side owns with @JoinColumn, other side uses mappedBy [OK]
Hint: Foreign key side owns with @JoinColumn, other side uses mappedBy [OK]
Common Mistakes:
- Assigning owning side incorrectly
- Placing mappedBy on owning side
- Confusing which table holds foreign key
