When an object is retrieved from the datastore by JPA typically not all fields are retrieved
immediately. This is because for efficiency purposes only particular field types are retrieved in the
initial access of the object, and then any other objects are retrieved when accessed (lazy loading).
The group of fields that are loaded is called a
There are 2 types of "fetch groups" to consider
JPA provides an initial fetch group, comprising the fields that will be retrieved when an object
is retrieved if the user does nothing to define the required behaviour. You define this "default"
by setting the
attribute in metadata for each field/property.
As mentioned above, JDO allows specification of users own fetch groups (for enabling at particular
times) and DataNucleus makes that flexibility available for JPA via annotations.
For example, if we have the following class
and we want to have the
field loaded whenever we load objects of this class, we define
our annotations as
public class MyClass
So we have defined a fetch group called "otherfield" that just includes the field with name
. We can then use this at runtime in our persistence code.
EntityManagerImpl dnEM = (EntityManagerImpl)em;
... (load MyClass object)
By default the
will include the default fetch group. We have changed this above by
the fetch group "otherfield", so when we retrieve an object using this
we will be retrieving the fields
are both in the current
. We can take the above much further than what is shown by
defining nested fetch groups in the MetaData. In addition we can change the
operation to control what is fetched during that operation.
The user has full flexibility to add many groups to the current
This gives much power and control over what will be loaded and when. A big improvement over
applies not just to calls to
, but also
, as well as detaching.