Entity relationships are one of the most important concepts in Spring Data JPA and Hibernate. Real-world applications rarely contain isolated tables. Instead, tables are usually connected to each other through relationships.
One-to-One mapping is the simplest relationship type and is frequently asked in Spring Boot and Hibernate interviews.
What is One-to-One Mapping?
A One-to-One relationship means that one record in a table is associated with exactly one record in another table.
In simple terms:
One Employee
↔
One Passport
An employee can have only one passport and a passport can belong to only one employee.
Real World Examples
- Employee ↔ Passport
- User ↔ Profile
- Person ↔ Aadhaar Card
- Student ↔ Library Card
- Customer ↔ Account
Database Structure
Employee Table
employee
id
name
department
passport_id
Passport Table
passport
id
passport_number
country
The foreign key passport_id creates the relationship.
Passport Entity
@Entity
@Table(name = "passport")
public class Passport {
@Id
@GeneratedValue(
strategy =
GenerationType.IDENTITY
)
private Long id;
private String passportNumber;
private String country;
}
Employee Entity
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(
strategy =
GenerationType.IDENTITY
)
private Long id;
private String name;
private String department;
@OneToOne
@JoinColumn(
name = "passport_id"
)
private Passport passport;
}
Understanding @OneToOne
The @OneToOne annotation tells JPA that the relationship between Employee and Passport is one-to-one.
@OneToOne
Spring Data JPA uses this annotation to generate the correct relationship mapping.
Understanding @JoinColumn
@JoinColumn specifies the foreign key column used to connect both tables.
@JoinColumn(
name = "passport_id"
)
This creates the passport_id foreign key inside the employee table.
Saving Data
Create a Passport object first.
Passport passport =
new Passport();
passport.setPassportNumber(
"P123456"
);
passport.setCountry(
"India"
);
Now create an Employee.
Employee employee =
new Employee();
employee.setName(
"John"
);
employee.setDepartment(
"IT"
);
employee.setPassport(
passport
);
Repository Layer
Employee Repository
@Repository
public interface EmployeeRepository
extends JpaRepository<
Employee,
Long> {
}
Passport Repository
@Repository
public interface PassportRepository
extends JpaRepository<
Passport,
Long> {
}
Using Cascade Operations
Without Cascade, Passport must be saved separately.
Cascade allows related entities to be persisted automatically.
@OneToOne(
cascade =
CascadeType.ALL
)
Now saving Employee automatically saves Passport.
Full Example
@OneToOne(
cascade =
CascadeType.ALL
)
@JoinColumn(
name = "passport_id"
)
private Passport passport;
employeeRepository
.save(employee);
Both records are saved together.
Generated Tables
passport
id
passport_number
country
employee
id
name
department
passport_id
Fetch Types
JPA supports two fetch strategies.
Eager Fetch
@OneToOne(
fetch =
FetchType.EAGER
)
Passport data loads immediately whenever Employee is fetched.
Lazy Fetch
@OneToOne(
fetch =
FetchType.LAZY
)
Passport data loads only when needed.
Unidirectional Mapping
In a unidirectional relationship, Employee knows about Passport but Passport does not know about Employee.
Employee
|
v
Passport
This is the simplest approach.
Bidirectional Mapping
Both entities know about each other.
Employee
↔
Passport
Passport Entity
@OneToOne(
mappedBy =
"passport"
)
private Employee employee;
Bidirectional mapping allows navigation from both sides.
Common Interview Questions
What is One-to-One Mapping?
A relationship where one entity is associated with exactly one other entity.
What does @JoinColumn do?
It defines the foreign key column used to establish the relationship.
What is CascadeType.ALL?
It propagates database operations from parent to child entities.
What is the difference between EAGER and LAZY loading?
EAGER loads related entities immediately while LAZY loads them only when required.
What is a Bidirectional Relationship?
Both entities maintain references to each other.
Best Practices
- Prefer LAZY loading whenever possible.
- Use CascadeType carefully.
- Avoid unnecessary bidirectional relationships.
- Keep relationships simple and clear.
- Use DTOs for API responses.
Frequently Asked Questions (FAQ)
Can One-to-One mapping have a foreign key?
Yes. One table typically contains the foreign key referencing the other table.
Can One-to-One mapping be bidirectional?
Yes. Both entities can reference each other.
Should I use EAGER or LAZY fetch?
LAZY is generally preferred for better performance.
Conclusion
One-to-One Mapping is the simplest relationship type in Spring Data JPA. Understanding @OneToOne, @JoinColumn, Cascade Types, Fetch Types and entity ownership is essential for designing database relationships and succeeding in Spring Boot interviews.
Keywords
OneToOne Mapping JPA, Spring Data JPA Relationships, @OneToOne Annotation, JPA One To One Example, Hibernate Relationships, Spring Boot JPA Tutorial, JPA Interview Questions, CascadeType Example, FetchType Lazy, FetchType Eager, Entity Relationships, Spring Data JPA Tutorial, Hibernate Mapping, Java Backend Development, Spring Boot Database Design
Reviewed by 13thGhost
on
10:35 PM
Rating:
