Performance: @OneToOne relationship
This affects database query performance and page load speed when fetching related entities in a web app.
Jump into concepts and practice - no test required
@OneToOne(fetch = FetchType.LAZY) private Profile profile;
@OneToOne(fetch = FetchType.EAGER) private Profile profile;
| Pattern | DB Queries | Blocking Time | Page Load Impact | Verdict |
|---|---|---|---|---|
| Eager @OneToOne | 1 join or 2 queries immediately | Blocks rendering until complete | Slower LCP, higher initial load | [X] Bad |
| Lazy @OneToOne | Query only when accessed | Non-blocking initial render | Faster LCP, better user experience | [OK] Good |
@OneToOne annotation represent in Spring Boot JPA?@OneToOne relationship to specify the foreign key column?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
}@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;
}@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?