Discover how smart data loading can make your app lightning fast and efficient!
Fetch types (LAZY vs EAGER) in Spring Boot - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a list of books, and each book has many authors. You want to show just the book titles on a page. If you manually load all authors for every book upfront, it feels like carrying a heavy backpack full of unnecessary stuff.
Loading all related data at once makes your app slow and uses too much memory. If you only need some data, fetching everything wastes time and resources. Manually controlling this is tricky and error-prone.
Fetch types like LAZY and EAGER let Spring Boot decide when to load related data. LAZY loads data only when needed, like unpacking your backpack only when you want something. EAGER loads everything immediately, useful when you know you need all data.
List<Book> books = bookRepository.findAll(); // manually fetch authors for each book later@OneToMany(fetch = FetchType.LAZY) List<Author> authors; // authors load only when accessed
This lets your app run faster and use less memory by loading data smartly, improving user experience and scalability.
When showing a list of blog posts, you load just the posts first (LAZY). Only when a user clicks a post, you load comments and details, saving bandwidth and speeding up the page.
Manual data loading can slow apps and waste resources.
LAZY fetch loads related data only when needed.
EAGER fetch loads all related data immediately.
Practice
LAZY fetch type do in Spring Boot JPA?Solution
Step 1: Understand fetch types in JPA
Fetch types control when related data is loaded from the database.Step 2: Define LAZY fetch behavior
LAZY means related entities are loaded only when you access them, not immediately.Final Answer:
It delays loading related entities until they are accessed. -> Option CQuick Check:
LAZY = delayed loading [OK]
- Confusing LAZY with EAGER loading
- Thinking LAZY loads data immediately
- Assuming LAZY disables loading
Solution
Step 1: Identify correct annotation and fetch type
EAGER fetch type is set usingfetch = FetchType.EAGERinside relationship annotations.Step 2: Match correct relationship and fetch type
@ManyToOne(fetch = FetchType.EAGER) uses@ManyToOne(fetch = FetchType.EAGER), which is valid and correct.Final Answer:
@ManyToOne(fetch = FetchType.EAGER) -> Option AQuick Check:
EAGER fetch uses FetchType.EAGER [OK]
- Using LAZY instead of EAGER for eager loading
- Mixing relationship types with wrong fetch types
- Forgetting to specify fetch attribute
order.getItems() is called if items is marked with fetch = FetchType.LAZY?
@Entity
class Order {
@OneToMany(fetch = FetchType.LAZY)
private List<Item> items;
}
Order order = entityManager.find(Order.class, 1L);
// No call to getItems() yet
Solution
Step 1: Understand LAZY fetch behavior on collections
With LAZY fetch, related collections likeitemsare not loaded immediately.Step 2: When is data loaded?
The data loads only when the gettergetItems()is called, triggering the fetch.Final Answer:
The items list is loaded only when getItems() is called. -> Option DQuick Check:
LAZY fetch loads on access [OK]
- Assuming collections load immediately with LAZY
- Expecting null instead of a proxy collection
- Confusing LAZY with EAGER behavior
@OneToMany(fetch = FetchType.LAZY) relationship, but you get a LazyInitializationException when accessing the collection outside a transaction. What is the likely cause?Solution
Step 1: Understand LazyInitializationException cause
This exception happens when a LAZY collection is accessed outside an open session or transaction.Step 2: Identify session state during access
If the session is closed before accessing the collection, the proxy cannot load data, causing the exception.Final Answer:
The collection was accessed after the session was closed. -> Option BQuick Check:
LazyInitializationException = access after session close [OK]
- Thinking fetch type alone prevents exceptions
- Ignoring session lifecycle in JPA
- Blaming database connection instead
Author with a @OneToMany(fetch = FetchType.LAZY) relationship to Book. You want to display all authors with their books immediately to avoid multiple queries later. Which approach is best?Solution
Step 1: Understand pros and cons of fetch types
Changing to EAGER can cause performance issues if not always needed.Step 2: Use JOIN FETCH for selective eager loading
JPQL with JOIN FETCH loads related entities eagerly only when needed, avoiding multiple queries.Step 3: Evaluate other options
Loading separately or using native queries is less efficient or more complex.Final Answer:
Keep LAZY fetch and use a JPQL query with JOIN FETCH to load books eagerly. -> Option AQuick Check:
JOIN FETCH = selective eager loading [OK]
- Switching to EAGER globally causing overhead
- Ignoring JPQL JOIN FETCH option
- Overcomplicating with native queries
