2.10 Fetching Strategies (Lazy vs Eager Loading) (Week 2 • JPA & Hibernate)
1. Core Concept & Internal Working
Fetching Strategies (Lazy vs Eager Loading) (JPA & Hibernate) covers JPA defaults (`@ManyToOne`/`@OneToOne` default to EAGER!; `@OneToMany`/`@ManyToMany` default to LAZY), why ALL associations should be `LAZY`, `LazyInitializationException`. In production software engineering, it is used to Explicitly setting `fetch = FetchType.LAZY` on every `@ManyToOne` and `@OneToOne` so loading an `Employee` never drags in the entire object graph unintentionally! Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Explicitly setting `fetch = FetchType.LAZY` on every `@ManyToOne` and `@OneToOne` so loading an `Employee` never drags in the entire object graph unintentionally!
3. Granular Subtopics Covered
- JPA defaults (`@ManyToOne`/`@OneToOne` default to EAGER!: Core foundation of Fetching Strategies (Lazy vs Eager Loading): Explicitly setting `fetch = FetchType.LAZY` on every `@ManyToOne` and `@OneToOne` so loading an `Employee` never drags in the entire object graph unintentionally!
- `@OneToMany`/`@ManyToMany` default to LAZY): Master '`@OneToMany`/`@ManyToMany` default to LAZY)' inside Fetching Strategies (Lazy vs Eager Loading) across real-world inputs.
- Why ALL associations should be `LAZY`: Master 'Why ALL associations should be `LAZY`' inside Fetching Strategies (Lazy vs Eager Loading) across real-world inputs.
- `LazyInitializationException`: Master '`LazyInitializationException`' inside Fetching Strategies (Lazy vs Eager Loading) across real-world inputs.
- Production Pitfall Guard (Fetching Strategies (Lazy vs Eager Loading)): Prevent common defect: Leaving `@ManyToOne` at its JPA default `FetchType.EAGER` or serializing lazy JPA entities directly to JSON after the transaction closes (`LazyInitializationException`).
- Hands-On Verification (Fetching Strategies (Lazy vs Eager Loading)): Override `@ManyToOne` to `LAZY`, reproduce `LazyInitializationException`, and fix it cleanly with `JOIN FETCH` / DTO projection.
4. Reference Implementation
// Golden JPA Rule: Set FetchType.LAZY on @ManyToOne and @OneToOne; fetch associations per use-case via JOIN FETCH or DTOs
5. Common Mistakes to Avoid
- Leaving `@ManyToOne` at its JPA default `FetchType.EAGER` or serializing lazy JPA entities directly to JSON after the transaction closes (`LazyInitializationException`).
- Skipping boundary, null/empty, or error-state verification when implementing Fetching Strategies (Lazy vs Eager Loading).
- Pushing untested Fetching Strategies (Lazy vs Eager Loading) changes without clean naming and Git commit context.
6. Hands-On Exercise
Override `@ManyToOne` to `LAZY`, reproduce `LazyInitializationException`, and fix it cleanly with `JOIN FETCH` / DTO projection.
7. Technical Interview Checkpoints
- Q: What problem does Fetching Strategies (Lazy vs Eager Loading) solve in JPA & Hibernate, and what fails if it is misused?
Key Focus: Explicitly setting `fetch = FetchType.LAZY` on every `@ManyToOne` and `@OneToOne` so loading an `Employee` never drags in the entire object graph unintentionally! Guard against: Leaving `@ManyToOne` at its JPA default `FetchType.EAGER` or serializing lazy JPA entities directly to JSON after the transaction closes (`LazyInitializationException`). - Q: Explain the internal working of Fetching Strategies (Lazy vs Eager Loading) (JPA defaults (`@ManyToOne`/`@OneToOne` default to EAGER!; `@OneToMany`/`@ManyToMany` default to LAZY), why ALL associations should be `LAZY`, `LazyInitializationException`).
Key Focus: Walk step-by-step through JPA defaults (`@ManyToOne`/`@OneToOne` default to EAGER!; `@OneToMany`/`@ManyToMany` default to LAZY), why ALL associations should be `LAZY`, `LazyInitializationException` with a concrete code example. - Q: How did you apply and verify Fetching Strategies (Lazy vs Eager Loading) in your hands-on lab?
Key Focus: Override `@ManyToOne` to `LAZY`, reproduce `LazyInitializationException`, and fix it cleanly with `JOIN FETCH` / DTO projection.