See also:
Description: This is an application meant to reveal the differences between JOIN
and JOIN FETCH
. The important thing to keep in mind is that, in case of LAZY
fetching, JOIN
will not be capable to initialize the associated collections along with their parent objects using a single SQL SELECT
. On the other hand, JOIN FETCH
is capable to accomplish this kind of task. But, don't underestimate JOIN
, because JOIN
is the proper choice when we need to combine/join the columns of two (or more) tables in the same query, but we don't need to initialize the associated collections on the returned entity (e.g., very useful for fetching DTO).
Key points:
- define two related entities (e.g.,
Author
andBook
in a one-to-many lazy-bidirectional relationship) - write a JPQL
JOIN
andJOIN FETCH
to fetch an author including his books - write a JPQL
JOIN
to fetch a book (1) - write a JPQL
JOIN
to fetch a book including its author (2) - write a
JOIN FETCH
to fetch a book including its author
Notice that:
- via
JOIN
, fetchingBook
ofAuthor
requires additionalSELECT
statements being prone to N+1 performance penalty - via
JOIN
(1), fetchingAuthor
ofBook
requires additionalSELECT
statements being prone to N+1 performance penalty - via
JOIN
(2), fetchingAuthor
ofBook
works exactly asJOIN FETCH
(requires a singleSELECT
) - via
JOIN FETCH
, fetching eachAuthor
of aBook
requires a singleSELECT