Fetch all collections eagerly in JPA

19,480

Solution 1

You can use join fetching, batch fetching, or load groups.

I would recommend batch fetching over join fetching, as it will perform significantly better with multiple collections.

See,

http://java-persistence-performance.blogspot.com/2010/08/batch-fetching-optimizing-object-graph.html

Also, http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/q_batch.htm#batch

http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/q_join_fetch.htm#fetch

http://wiki.eclipse.org/EclipseLink/Examples/JPA/AttributeGroup#Load_Examples

Solution 2

Nested fetch joins are not allowed by JPA, you should use the EclipseLink query hint eclipselink.join-fetch. See the answer to this question.

Solution 3

If I recall correctly, you need to use FETCH JOIN statements in your query.

See also: Fetch Joins

Share:
19,480
Naveed S
Author by

Naveed S

A J2EE developer currently working in travel domain. Highly passionate about researching and finding new things, learning from others and providing my knowledge to those in need.

Updated on June 04, 2022

Comments

  • Naveed S
    Naveed S almost 2 years

    Is it possible to fetch all collections in an entity hierarchy eagerly in a particular query only?

    For example,

    entity Department has a list of entity Employee. Employee has a list of entity Address and a list of entity Credentials. Department has another list of entity Project.

    I want to eagerly fetch all these collections (in my real case there are more collections) in a particular query SELECT dept FROM Department dept and not in other cases (so can't be annotated with FetchType.EAGER). Is it possible?

    I am using Eclipselink.

    Thanks in advance.