Program guide > Access data with client applications > Cache objects and their relationships (EntityManager API)
EntityManager fetch plan support
A FetchPlan is the strategy that the entity manager uses for retrieving associated objects if the application needs to access relationships.
Example
Assume for example that the application has two entities: Department and Employee. The relationship between the Department entity and the Employee entity is a bi-directional one-to-many relationship: One department has many employees, and one employee belongs to only one department. Since most of the time, when Department entity is fetched, its employees are likely to be fetched, the fetch type of this one-to-many relationship is set to be EAGER.
Here is a snippet of the Department class.
@Entity public class Department { @Id private String deptId; @Basic String deptName; @OneToMany(fetch = FetchType.EAGER, mappedBy="department", cascade = {CascadeType.PERSIST}) public Collection<Employee> employees; }
In a distributed environment, when an application calls em.find(Department.class, "dept1") to find a Department entity with key "dept1", this find operation will get the Department entity and all its eager-fetched relations. In the case of the preceding snippet, these are all the employees of department "dept1".
Prior to WebSphere eXtreme Scale 6.1.0.5, the retrieval of one Department entity and N Employee entities incurred N+1 client-server trips because the client retrieved one entity for one client-server trip. You can improve performance if you retrieve these N+1 entities in one trip.
Fetch plan
A fetch plan can be used to customize how to fetch eager relationships by customizing the maximum depth of the relationships. The fetch depth overrides eager relations greater than the specified depth to lazy relations. By default, the fetch depth is the maximum fetch depth. This means that eager relationships of all levels that are eager-navigable from the root entity will be fetched. An EAGER relationship is eager-navigable from a root entity if and only if all the relations starting from the root entity to it are configured as eager-fetched.
In the previous example, the Employee entity is eager-navigable from the Department entity because the Department-Employee relationship is configured as eager-fetched.
If the Employee entity has another eager relationship to an Address entity for instance, then the Address entity is also eager-navigable from the Department entity. However, if the Department-Employee relationships were configured as lazy-fetch, then the Address entity is not eager-navigable from the Department entity because the Department-Employee relationship breaks the eager fetch chain.
A FetchPlan object can be retrieved from the EntityManager instance. The application can use the setMaxFetchDepth method to change the maximum fetch depth.
A fetch plan is associated with an EntityManager instance. The fetch plan applies to any fetch operation, more specifically as follows.
- EntityManager find(Class class, Object key) and findForUpdate(Class class, Object key) operations
- Query operations
- QueryQueue operations
The FetchPlan object is mutable. Once changed, the changed value will be applied to the fetch operations executed afterward.
A fetch plan is important for a distributed deployment because it decides whether the eager-fetched relationship entities are retrieved with the root entity in one client-server trip or more than one.
Continuing with the previous example, consider further that the fetch plan has maximum depth set to infinity. In that case, when an application calls em.find(Department.class, "dept1") to find a Department, this find operation will get one Department entity and N employee entities in one client-server trip. However, for a fetch plan with maximum fetch depth set to zero, only the Department object will be retrieved from the server, while the Employee entities are retrieved from the server only when the employees collection of the Department object is accessed.
Different fetch plans
You have several different fetch plans based on the requirements, explained in the following sections.
Impact on a distributed grid
- Infinite-depth fetch plan: An infinite-depth fetch plan has its maximum fetch depth set to FetchPlan.DEPTH_INFINITE.
In a client-server environment, if an infinite-depth fetch plan is used, then all the relations that are eager-navigable from the root entity will be retrieved in one client-server trip.
Example: If the application is interested in all the Address entities of all employees of a particular Department, then it uses an infinite-depth fetch plan to retrieve all the associated Address entities. The following code only incurs one client-server trip.
em.getFetchPlan().setMaxFetchDepth(FetchPlan.DEPTH_INFINITE); tran.begin(); Department dept = (Department) em.find(Department.class, "dept1"); // do something with Address object. for (Employee e: dept.employees) { for (Address addr: e.addresses) { // do something with addresses. } } tran.commit();
- Zero-depth fetch plan: A zero-depth fetch plan has its maximum fetch depth set to 0.
In a client-server environment, if a zero fetch plan is used, then only the root entity will be retrieved in the first client-server trip. All the eager relationships are treated as if they were lazy.
Example: In this example, the application is only interested in the Department entity attribute. It does not need to access its employees, so the application sets the fetch plan depth to 0.
Session session = objectGrid.getSession(); EntityManager em = session.getEntityManager(); EntityTransaction tran = em.getTransaction(); em.getFetchPlan().setMaxFetchDepth(0); tran.begin(); Department dept = (Department) em.find(Department.class, "dept1"); // do something with dept object. tran.commit();
- Fetch plan with depth k :
A k-depth fetch plan has its maximum fetch depth set to k.
In a client-server eXtreme Scale environment, if a k-depth fetch plan is used, then all the relationships eager-navigable from the root entity within k steps will be retrieved in the first client-server trip.
The infinite-depth fetch plan (k = infinity) and zero-depth fetch plan (k = 0) are just two examples of the k-depth fetch plan.
To continue expanding on the previous example, assume there is another eager relationship from the entity Employee to the entity Address. If the fetch plan has maximum fetch depth set to 1, then the em.find(Department.class, "dept1") operation will retrieve the Department entity and all its Employee entities in one client-server trip. However, the Address entities will not be retrieved because they are not eager-navigable to the Department entity within 1 step, but 2 steps.
If you use a fetch plan with depth set to 2, then the em.find(Department.class, "dept1") operation will retrieve the Department entity, all its Employee entities, and all Address entities associated with the Employee entities in one client-server trip.
Tip: The default fetch plan has maximum fetch depth set to infinity, so the default behavior of a fetch operation can change. All the eager-navigable relationships from the root entity are retrieved. Instead of multiple trips, now the fetch operation only incurs one client-server trip with the default fetch plan.
To keep the settings for the product from the prior version, set the fetch depth to 0.
- Fetch plan used on query:
If you execute an entity query you can also use a fetch plan to customize relationship retrieval.
For example, the query SELECT d FROM Department d WHERE "d.deptName='Department'" result has a relationship to the Department entity. Notice the fetch plan depth starts with the query result association: In this case, the Department entity, not the query result itself. That is, the Department entity is on fetch-depth level 0. Therefore a fetch plan with maximum fetch depth 1 will retrieve the Department entity and its Employee entities in one client-server trip.
Example: In this example, the fetch plan depth is set to 1, so the Department entity and its Employee entities are retrieved in one client-server trip, but the Address entities will not be retrieved in the same trip.
If a relationship is ordered, using either OrderBy annotation or configuration, then it is considered an eager relationship even if it is configured as lazy-fetch.
Performance considerations in a distributed environment
By default, all relationships that are eager-navigable from the root entity will be retrieved in one client-server trip. This can improve performance if all the relationships are going to be used. However, in certain usage scenarios, not all relationships eager-navigable from the root entity are used, so they incur both run-time overhead and bandwidth overhead by retrieving those unused entities.
For such cases, the application can set the maximum fetch depth to a small number to decrease the depth of entities to be retrieved by making all the eager relations after that certain depth lazy. This setting can improve performance.
Proceeding still further with the previous Department-Employee-Address example, by default, all the Address entities associated with employees of the Department "dept1" will be retrieved when em.find(Department.class, "dept1") is called. If the application does not use Address entities, it can set the maximum fetch depth to 1, so the Address entities will not be retrieved with the Department entity.
Parent topic:
Cache objects and their relationships (EntityManager API)
Related concepts
EntityManager in a distributed environment
Interacting with EntityManager
EntityManager interface performance impact
Related tasks
Entity manager tutorial: Overview
Related reference