The JPAEntityLoader plug-in is a built-in Loader implementation that uses Java™ Persistence API (JPA) to communicate with the database when you are using the EntityManager API. When you are using the ObjectMap API, use the JPALoader loader.
Use the JPALoader plug-in when you are storing data using the ObjectMap API. Use the JPAEntityLoader plug-in when you are storing data using the EntityManager API.
com.ibm.websphere.objectgrid.em.EntityManager em = og.getSession().getEntityManager(); em.getTransaction().begin(); Consumer c1 = (Consumer) em.find(Consumer.class, c.getConsumerId()); c1.setName("New Name"); em.getTransaction().commit();
In this example, an update type LogElement is sent to the JPAEntityLoader of the map consumer. The javax.persistence.EntityManager.merge(Object o) method is called to the JPA entity manager instead of an attribute update to the JPA-managed entity. Because of this changed behavior, some limitations exist with using this programming model.
Entities have relationships with other entities. Designing an application with relationships involved and with JPAEntityLoader plugged in requires additional considerations. The application should follow the following four rules, described in the following sections.
The JPAEntityLoader is only supported when using entities without any relationships or entities with single-level relationships. Relationships with more than one level, such as Company > Department > Employee are not supported.
Using the Consumer-ShippingAddress entity relationships as an example, when you load a consumer with eager fetch enabled, you could load all the related ShippingAddress objects. When you persist or merge a Consumer object, you could persist or merge related ShippingAddress objects if cascade-persist or cascade-merge is enabled.
You cannot plug in a loader for the root entity map which stores the Consumer entity tuples. You must configure a loader for each entity map.
Reconsider the scenario where the entity Consumer has a one-to-many relationship with ShippingAddress. You can look at the scenario where cascade-persist is enabled for this relationship. When a Consumer object is persisted into eXtreme Scale, the associated N number of ShippingAddress objects are also persisted into eXtreme Scale.
A persist call of the Consumer object with a cascade-persist relationship to ShippingAddress translates to one javax.persistence.EntityManager.persist(consumer) method call and N javax.persistence.EntityManager.persist(shippingAddress) method calls by the JPAEntityLoader layer. However, these N extra persist calls to ShippingAddress objects are unnecessary because of the cascade-persist setting from the JPA provider point of view. To solve this problem, eXtreme Scale provides a new method isCascaded on the LogElement interface. The isCascaded method indicates whether the LogElement is a result of an eXtreme Scale EntityManager cascade operation. In this example, the JPAEntityLoader of the ShippingAddress map receives N LogElement objects because of the cascade persist calls. The JPAEntityLoader finds out that the isCascaded method returns true and then ignores them without making any JPA calls. Therefore, from a JPA point of view, only one javax.persistence.EntityManager.persist(consumer) method call is received.
The same behavior is exhibited if you merge an entity or remove an entity with cascade enabled. The cascaded operations are ignored by the JPAEntityLoader plug-in.
The design of the cascade support is to replay the eXtreme Scale EntityManager operations to the JPA providers. These operations include persist, merge, and remove operations. To enable cascade support, verify that the cascade setting for the JPA and the eXtreme Scale EntityManager are the same.
As previously described, the design of the cascade support is to replay eXtreme Scale EntityManager operations to the JPA providers. If your application calls the ogEM.persist(consumer) method to the eXtreme Scale EntityManager, even the associated ShippingAddress objects are persisted because of the cascade-persist setting, and the JPAEntityLoader only calls the jpAEM.persist(consumer) method to the JPA providers.
However, if your application updates a managed entity, this update translates to a JPA merge call by the JPAEntityLoader plug-in. In this scenario, support for multiple levels of relationships and key associations is not guaranteed. In this case, the best practice is to use the javax.persistence.EntityManager.merge(o) method instead of updating a managed entity.