0
0
Spring Bootframework~10 mins

Fetch types (LAZY vs EAGER) in Spring Boot - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Fetch types (LAZY vs EAGER)
Start Entity Load
Fetch Type?
EAGERLoad Related Data Immediately
Load Proxy Placeholder
Access Related Data?
NoReturn Proxy
Yes
Fetch Related Data Now
Return Full Data
When loading an entity, eager fetch loads related data immediately, while lazy fetch loads a placeholder and fetches related data only when accessed.
Execution Sample
Spring Boot
class Order {
  @OneToMany(fetch = FetchType.LAZY)
  List<Item> items;
}

Order order = repo.findById(1).orElse(null);
order.getItems();
This code loads an Order entity with items fetched lazily, so items load only when getItems() is called.
Execution Table
StepActionFetch TypeData LoadedResult
1Load Order entityLAZYOrder data onlyOrder object with items proxy
2Access order.getItems()LAZYItems data fetched nowFull Order with items loaded
3Load Order entityEAGEROrder and items dataFull Order with items loaded immediately
4Access order.getItems()EAGERNo additional fetchItems already loaded
💡 Execution stops after data is fully loaded or proxy returned depending on fetch type.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
ordernullOrder with items proxyOrder with items loadedOrder with items loadedOrder with items loaded
Key Moments - 2 Insights
Why does accessing items trigger a database call in LAZY fetch?
Because at Step 1 in execution_table, only a proxy is loaded. The actual items data is fetched only when getItems() is called at Step 2.
Does EAGER fetch load related data immediately or later?
EAGER fetch loads related data immediately as shown in Step 3, so no extra fetch happens when accessing items at Step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is loaded at Step 1 with LAZY fetch?
AOrder data only with items proxy
BOrder and items data fully loaded
COnly items data loaded
DNo data loaded yet
💡 Hint
Check Step 1 row under Data Loaded column in execution_table.
At which step does the actual items data load in LAZY fetch?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at when 'Items data fetched now' happens in execution_table.
If fetch type changes from LAZY to EAGER, what changes in the execution?
AItems load only when accessed at Step 2
BItems load immediately with Order at Step 3
COrder does not load at all
DItems never load
💡 Hint
Compare Step 3 and Step 1 rows for EAGER vs LAZY in execution_table.
Concept Snapshot
Fetch Types in Spring Boot:
- LAZY: Load main entity first, related data loads only on access.
- EAGER: Load main entity and related data immediately.
- LAZY uses proxy placeholders to delay loading.
- EAGER fetch can impact performance if related data is large.
- Choose fetch type based on use case to optimize loading.
Full Transcript
In Spring Boot, fetch types control when related data loads with an entity. EAGER fetch loads all related data immediately when the main entity loads. LAZY fetch loads only the main entity first and uses a proxy for related data. The related data loads only when accessed later. This helps optimize performance by delaying data loading until needed. The execution table shows that with LAZY, the items list is a proxy after loading the order, and only fetches items when getItems() is called. With EAGER, items load immediately with the order. Understanding this helps avoid unexpected database calls and performance issues.