Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the @OneToOne annotation represent in Spring Boot?
It represents a one-to-one relationship between two entities, meaning each instance of one entity is linked to exactly one instance of another entity.
Click to reveal answer
intermediate
How do you specify the owning side in a @OneToOne relationship?
The owning side is the entity that contains the foreign key and uses the @JoinColumn annotation to specify the column that joins the two tables.
Click to reveal answer
intermediate
What is the purpose of the mappedBy attribute in a @OneToOne relationship?
It is used on the inverse side to indicate that the relationship is controlled by the other entity's field, avoiding duplicate foreign keys.
Click to reveal answer
advanced
Can a @OneToOne relationship be lazy loaded in Spring Boot?
Yes, by default @OneToOne is eager, but you can set fetch=FetchType.LAZY to load the related entity only when accessed.
Click to reveal answer
beginner
What happens if you don't specify @JoinColumn in a @OneToOne relationship?
Spring Boot will create a default foreign key column with a generated name, which might not match your database schema or naming conventions.
Click to reveal answer
In a @OneToOne relationship, which side owns the foreign key?
AThe side with the @JoinColumn annotation
BThe side with the mappedBy attribute
CBoth sides share the foreign key equally
DNeither side owns the foreign key
✗ Incorrect
The owning side is the entity with the @JoinColumn annotation that holds the foreign key.
What does the mappedBy attribute do in a @OneToOne relationship?
ARemoves the relationship
BDefines the owning side of the relationship
CCreates a new foreign key column
DSpecifies the inverse side and points to the owning side's field
✗ Incorrect
mappedBy tells Spring Boot which field owns the relationship to avoid duplicate foreign keys.
How do you make a @OneToOne relationship load lazily?
ALazy loading is not possible with @OneToOne
BUse @Lazy annotation on the entity class
CSet fetch=FetchType.LAZY on the @OneToOne annotation
DSet fetch=FetchType.EAGER
✗ Incorrect
Setting fetch=FetchType.LAZY delays loading the related entity until it is accessed.
What is the default fetch type for @OneToOne in Spring Boot?
AEAGER
BNONE
CLAZY
DMANUAL
✗ Incorrect
By default, @OneToOne relationships are eagerly fetched.
If you omit @JoinColumn in a @OneToOne relationship, what happens?
AAn error occurs at runtime
BSpring Boot creates a default foreign key column
CThe relationship is ignored
DThe database schema is not updated
✗ Incorrect
Spring Boot generates a default foreign key column name if @JoinColumn is not specified.
Explain how to set up a @OneToOne relationship between two entities in Spring Boot, including owning and inverse sides.
Think about which entity holds the foreign key and how to avoid duplicate keys.
You got /5 concepts.
Describe the difference between the owning side and the inverse side in a @OneToOne relationship.
Consider who 'owns' the link in the database.
You got /4 concepts.
Practice
(1/5)
1. What does the @OneToOne annotation represent in Spring Boot JPA?
easy
A. A relationship where one entity is linked to exactly one other entity
B. A relationship where one entity is linked to many entities
C. A relationship where many entities are linked to many entities
D. A relationship where entities are not linked at all
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 A
Quick 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
A. @MappedBy
B. @Column
C. @JoinColumn
D. @Entity
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 C
Quick 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
A. Throws NullPointerException because passport is not initialized
B. Returns the passport number linked to the person
C. Returns null because @OneToOne is missing mappedBy
D. Compilation error due to missing @MappedBy
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 B
Quick 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
A. Missing @JoinColumn on Profile entity owning side
B. mappedBy should be on Profile, not User
C. User entity should not have @OneToOne annotation
D. Profile entity must use mappedBy instead of @OneToOne
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 A
Quick 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
A. In Employee: @OneToOne(mappedBy = "desk") Desk desk; In Desk: @OneToOne @JoinColumn Employee employee;
B. In Employee: @OneToOne @JoinColumn Desk desk; In Desk: @OneToOne(mappedBy = "desk") Employee employee;
C. ;eeyolpme eeyolpmE nmuloCnioJ@ enOoTenO@ :kseD nI ;ksed kseD )"eeyolpme" = yBdeppam(enOoTenO@ :eeyolpmE nI
D. In Employee: @OneToOne(mappedBy = "employee") Desk desk; In Desk: @OneToOne @JoinColumn Employee employee;
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 D
Quick Check:
Foreign key side owns with @JoinColumn, other side uses mappedBy [OK]
Hint: Foreign key side owns with @JoinColumn, other side uses mappedBy [OK]