A FetchGroup is a performance enhancement that allows a group of attributes of an object to be loaded on demand, which means that the data for an attribute might not loaded from the underlying data source until an explicit access call for the attribute first occurs. It avoids loading all data of the object's attributes, in which the user is interested in only a subset of them. A great deal of caution and careful system use case analysis should be use when using the fetch group feature, as the extra round-trip would well offset the gain from the deferred loading in many cases.
FetchGroup usage is only possible when an entity class implements the {@link FetchGroupTracker} interface so that the FetchGroup can be stored inthe entity. The entity must also use the provided check methods to ensure the attributes are loaded prior to use. In general this support is enabled through weaving of the entity classes. If an entity class does not implement {@link FetchGroupTracker} no FetchGroup functionality will be supported andattempted use of a FetchGroup in a query will not result in the expected behavior.
FetchGroups are defined in 3 ways:
- A {@link FetchGroupManager#getDefaultFetchGroup()} is created and storedon the {@link FetchGroupManager} during metadata processing if any of thebasic ( {@link DirectToFieldMapping}) are configured to be loaded directly.
- A named FetchGroup can be defined and added to the {@link FetchGroupManager}. For JPA users this can be accomplished using annotation (@FetchGroup) or in an eclipselink-orm.xml. For JPA and native users named groups can be defined in code and added to the {@link FetchGroupManager#addFetchGroup(FetchGroup)}. Adding named groups in code is typically done in a {@link DescriptorCustomizer}and should be done before the session is initialized at login. To use a named FetchGroup on a query the native {@link ObjectLevelReadQuery#setFetchGroupName(String)} canbe used of for JPA users the {@link QueryHints#FETCH_GROUP_NAME} an be used.
- A dynamic FetchGroup can be created within the application and used on a query. For native API usage this is done using {@link ObjectLevelReadQuery#setFetchGroup(FetchGroup)} while JPA usersgenerally use the {@link QueryHints#FETCH_GROUP}.
When a query is executed only one FetchGroup will be used. The order of precedence is:
- If a FetchGroup is specified on a query it will be used.
- If no FetchGroup is specified but a FetchGroup name is specified and the FetchGroupManager has a group by this name it will be used.
- If neither a FetchGroup nor a FetchGroup name is specified on the query an the FetchGroupManager has a default group then it will be used.
- If none of these conditions are met then no FetchGroup will be used when executing a query.
Note: This includes the execution of queries to populate lazy and eager relationships. Loading: A FetchGroup can optionally specify that it needs its included relationships loaded. This can be done using {@link #setShouldLoad(boolean)} and {@link #setShouldLoadAll(boolean)} aswell as the corresponding configurations in the @FetchGroup annotation and the element in the eclipselink-orm.xml. When this si configured the FetchGroup will also function as a {@link LoadGroup} causing all of itsspecified relationships to be populated prior to returning the results form the query execution.
@see FetchGroupManager
@see QueryHints#FETCH_GROUP
@see LoadGroup
@author King Wang, dclarke, ailitchev
@since TopLink 10.1.3