How To Avoid LazyInitializationException Via JOIN FETCH
See also:
Description: Typically, when we get a LazyInitializationException we tend to modify the association fetching type from LAZY to EAGER. That is very bad! This is a code smell. Best way to avoid this exception is to rely on JOIN FETCH (if you plan to modify the fetched entities) or JOIN + DTO (if the fetched data is only read). JOIN FETCH allows associations to be initialized along with their parent objects using a single SELECT. This is particularly useful for fetching associated collections.
This application is a JOIN FETCH example for avoiding LazyInitializationException.
Key points:
- define two related entities (e.g.,
AuthorandBookin a@OneToManylazy-bidirectional association) - write a JPQL
JOIN FETCHto fetch an author including his books - write a JPQL
JOIN FETCH(orJOIN) to fetch a book including its author


