One of the most important performance-related concepts in Spring Data JPA is Fetch Strategy. The way Hibernate loads related entities can significantly impact application performance.

Understanding FetchType.LAZY and FetchType.EAGER is essential because incorrect usage often causes performance problems, excessive database queries and memory issues.
What is Fetching?
Fetching refers to the process of loading related entities from the database.
Whenever an entity contains a relationship, Hibernate must decide when the related data should be loaded.
Department
|
v
Employees
Should employees be loaded immediately or only when needed?
This decision is controlled by FetchType.
Types of Fetch Strategies
- FetchType.LAZY
- FetchType.EAGER
What is FetchType.LAZY?
LAZY loading delays the loading of related entities until they are actually needed.
@OneToMany(
fetch = FetchType.LAZY
)
Hibernate loads only the parent entity initially.
Related entities are fetched later when accessed.
Example of LAZY Loading
Department department =
departmentRepository
.findById(1L)
.get();
Initially Hibernate loads:
Department
Employees are not loaded yet.
department.getEmployees();
Now Hibernate fetches employees from the database.
What is FetchType.EAGER?
EAGER loading fetches related entities immediately along with the parent entity.
@OneToMany(
fetch = FetchType.EAGER
)
When Department is loaded, Employees are loaded automatically.
Example of EAGER Loading
Department department =
departmentRepository
.findById(1L)
.get();
Hibernate loads:
Department
Employees
Both entities are retrieved immediately.
Department Entity Example
@Entity
public class Department {
@Id
private Long id;
private String name;
@OneToMany(
mappedBy = "department",
fetch = FetchType.LAZY
)
private List<Employee>
employees;
}
Employee Entity Example
@Entity
public class Employee {
@Id
private Long id;
private String name;
@ManyToOne(
fetch = FetchType.EAGER
)
private Department department;
}
Default Fetch Types in JPA
| Relationship | Default Fetch Type |
|---|---|
| @OneToOne | EAGER |
| @ManyToOne | EAGER |
| @OneToMany | LAZY |
| @ManyToMany | LAZY |
Performance Problem with EAGER Loading
Suppose a Department contains 1000 employees.
Fetching Department automatically loads all employees.
Department
+
1000 Employees
This increases memory consumption and query execution time.
Advantages of LAZY Loading
- Improved performance
- Lower memory usage
- Faster initial queries
- Better scalability
Advantages of EAGER Loading
- Simple implementation
- No delayed loading
- Useful when related data is always required
LazyInitializationException
A common Hibernate exception occurs when a lazy-loaded entity is accessed outside the persistence context.
LazyInitializationException
Example
Department department =
repository.findById(1L)
.get();
session.close();
department.getEmployees();
Hibernate can no longer fetch employees because the session is closed.
Generated SQL (LAZY)
Query 1
SELECT *
FROM department
WHERE id = 1
Later Query
SELECT *
FROM employee
WHERE department_id = 1
Data is loaded only when required.
Generated SQL (EAGER)
SELECT *
FROM department
JOIN employee
ON department.id =
employee.department_id
All data loads immediately.
LAZY vs EAGER
| Feature | LAZY | EAGER |
|---|---|---|
| Loading Time | On Demand | Immediate |
| Performance | Better | Can Be Costly |
| Memory Usage | Lower | Higher |
| Scalability | Better | Depends on Data Size |
Common Interview Questions
What is FetchType.LAZY?
Related entities are loaded only when accessed.
What is FetchType.EAGER?
Related entities are loaded immediately with the parent entity.
Which fetch strategy is recommended?
LAZY loading is generally preferred for better performance.
What is LazyInitializationException?
It occurs when a lazy-loaded entity is accessed after the Hibernate session is closed.
What is the default fetch type for ManyToOne?
EAGER.
Best Practices
- Prefer LAZY loading whenever possible.
- Avoid unnecessary EAGER relationships.
- Monitor SQL queries generated by Hibernate.
- Be aware of LazyInitializationException.
- Load only the data you actually need.
Frequently Asked Questions (FAQ)
Should I always use LAZY loading?
Most of the time yes, but some use cases require immediate loading.
Can EAGER loading cause performance issues?
Yes. It may load large amounts of unnecessary data.
Can FetchType be overridden?
Yes. Developers can explicitly specify the required fetch strategy.
Conclusion
FetchType.LAZY and FetchType.EAGER directly impact application performance and memory consumption. Understanding when Hibernate loads related entities helps developers build efficient Spring Boot applications and avoid common persistence problems. Mastering Fetch Strategies is essential for enterprise development and Spring Data JPA interviews.
Keywords
FetchType.LAZY, FetchType.EAGER, Hibernate Fetch Strategies, Spring Data JPA Performance, Lazy Loading, Eager Loading, LazyInitializationException, JPA Interview Questions, Hibernate Optimization, Spring Boot JPA Tutorial, Entity Relationships, Database Performance, Hibernate Tutorial, Spring Data JPA Tutorial, Java Backend Development
Reviewed by 13thGhost
on
10:52 AM
Rating: