2.10.1.4. Fetch Joins

JPQL queries may specify one or more join fetch declarations, which allow the query to specify which fields in the returned instances will be pre-fetched.

SELECT x FROM Magazine x join fetch x.articles WHERE x.title = 'JDJ'

The query above returns Magazine instances and guarantees that the articles field will already be fetched in the returned instances.

Multiple fields may be specified in separate join fetch declarations:

SELECT x FROM Magazine x join fetch x.articles join fetch x.authors WHERE x.title = 'JDJ'

Notice that in the above query, both articles and authors are relation property in Magazine. JPQL syntax does not allow range variable declared for paths on the right-hand side of join fetch. Therefore, if Article entity has a relation property of publishers, it is not possible to specify a query that returns Magazine instances and pre-fetch the articles and the publishers. The following query will result in syntax error:

SELECT x FROM Magazine x join fetch x.articles a join fetch a.publishers p WHERE x.title = 'JDJ'
Note

Specifying the join fetch declaration is functionally equivalent to adding the fields to the Query’s FetchConfiguration. See Fetch Groups.