0
0
Spring Bootframework~10 mins

@OneToOne relationship in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @OneToOne relationship
Define Entity A
Define Entity B
Add @OneToOne in Entity A
Add @OneToOne in Entity B (optional)
Save Entity A with Entity B
Database stores linked records
Fetch Entity A retrieves Entity B automatically
This flow shows how two entities are linked one-to-one by adding @OneToOne annotations and saving them, so fetching one gets the other.
Execution Sample
Spring Boot
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Person {
  @Id
  private Long id;

  @OneToOne
  private Passport passport;

  // getters and setters
}

@Entity
public class Passport {
  @Id
  private Long id;

  @OneToOne(mappedBy = "passport")
  private Person person;

  // getters and setters
}
Defines two classes Person and Passport linked by a one-to-one relationship using @OneToOne.
Execution Table
StepActionEntity StateDatabase StateResult
1Create Person objectPerson(passport=null)EmptyPerson created with no passport linked
2Create Passport objectPassport(person=null)EmptyPassport created with no person linked
3Set Person.passport = PassportPerson(passport=Passport)EmptyPerson now references Passport
4Set Passport.person = PersonPassport(person=Person)EmptyPassport now references Person
5Save Person to DBPerson(passport=Passport)Person and Passport saved with linkDatabase stores linked records
6Fetch Person from DBPerson(passport=Passport)Person and Passport in DBPerson fetched with Passport linked
7Fetch Passport from DBPassport(person=Person)Person and Passport in DBPassport fetched with Person linked
8EndEntities linkedDatabase consistentOne-to-one relationship established
💡 Entities saved and linked; fetching one entity retrieves the other automatically.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Person.passportnullPassport objectPassport objectPassport objectPassport object
Passport.personnullnullPerson objectPerson objectPerson object
Key Moments - 3 Insights
Why do we set both sides of the relationship (Person.passport and Passport.person)?
Setting both sides keeps the Java objects in sync; execution_table steps 3 and 4 show this. Without both, one side may not reflect the link.
What happens if we save only Person without setting Passport.person?
The database will save the link from Person to Passport, but Passport.person may be null in Java until refreshed. See step 5 and 6 in execution_table.
Is the @OneToOne annotation required on both entities?
No, only one side needs @OneToOne; the other can use mappedBy to indicate the owner. This is shown in the sample code and steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of Passport.person?
Anull
BPerson object
CPassport object
DUndefined
💡 Hint
Check the 'Entity State' column at step 4 in execution_table.
At which step does the database first store the linked entities?
AStep 5
BStep 6
CStep 3
DStep 7
💡 Hint
Look for 'Database State' changes in execution_table rows.
If we skip setting Passport.person, what changes in the execution_table?
ADatabase does not save Passport
BPerson.passport becomes null
CPassport.person remains null after step 4
DPerson object is not created
💡 Hint
Refer to variable_tracker and step 4 in execution_table.
Concept Snapshot
@OneToOne links two entities so each has exactly one related entity.
Use @OneToOne on one side and optionally mappedBy on the other.
Set both sides in Java objects to keep them in sync.
Saving the owning side stores the link in the database.
Fetching one entity loads the linked entity automatically.
Full Transcript
The @OneToOne relationship in Spring Boot links two entities so each has exactly one related entity. You define two classes, for example Person and Passport, and add @OneToOne annotations to connect them. One side owns the relationship, the other uses mappedBy to point back. When you create objects, you set both sides to keep them in sync. Saving the owning entity stores both linked records in the database. Later, fetching one entity automatically loads the linked entity. This trace shows each step from creating objects, setting links, saving to the database, and fetching linked data. Key points are setting both sides and understanding which side owns the relationship.