Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing JPA query to return eagerly all collections / fields

I have few scenarios where the server queries objects from the database by using JPA and then sends the objects to the client over web services.
Since the client expects the full graph in such scenarios I would want to override the lazy loaded collections with eager loading and probably request for batch loading (for performance). Is there a way in JPA (or EclipseLink) to override the query in a generic manner (without rewriting the query) and request the full graph?

like image 893
Avner Levy Avatar asked Apr 08 '13 12:04

Avner Levy


1 Answers

10.1.3. 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'


Source : http://docs.oracle.com/cd/E13189_01/kodo/docs40/full/html/ejb3_overview_query.html#ejb3_overview_join_fetch

like image 148
Gab Avatar answered Oct 10 '22 08:10

Gab