[Java programming language only]

Configuring JPA loaders

A Java™ Persistence API (JPA) Loader is a plug-in implementation that uses JPA to interact with the database.

Before you begin

  • You must have a JPA implementation, such as Hibernate or OpenJPA. WebSphere® eXtreme Scale supports JPA 2.0.
    Note: For WebSphere eXtreme Scale under Liberty profile, JPA L2 is not supported.
  • Your database can be any back end that is supported by the chosen JPA provider.
  • Decide whether you are going to use the JPALoader plug-in or the JPAEntityLoader plug-in. 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.
    Note: If you are using the JPA APIs to access the JPA data source, use the JPA L2 cache plug-in. The cache plug-in introduces the data grid between your application and the JPA data source, while still using a JPA application. For more information, see JPA level 2 (L2) cache plug-in.

About this task

For more information about how the Java Persistence API (JPA) Loader works, see JPA Loaders.

Procedure

  1. Configure the necessary parameters that JPA requires to interact with a database.

    The following parameters are required. These parameters are configured in the JPALoader or JPAEntityLoader bean, and JPATxCallback bean.

    • persistenceUnitName: Specifies the persistence unit name. This parameter is required for two purposes: for creating a JPA EntityManagerFactory, and for locating the JPA entity metadata in the persistence.xml file. This attribute is set on the JPATxCallback bean.
    • JPAPropertyFactory: Specifies the factory to create a persistence property map to override the default persistence properties. This attribute is set on the JPATxCallback bean. To set this attribute, Spring style configuration is required.
    • entityClassName: Specifies the entity class name that is required to use JPA methods, such as EntityManager.persist, EntityManager.find, and so on. The JPALoader plug-in requires this parameter, but the parameter is optional for JPAEntityLoader. For the JPAEntityLoader plug-in, if an entityClassName parameter is not configured, the entity class configured in the ObjectGrid entity map is used. You must use the same class for the eXtreme Scale EntityManager and for the JPA provider. This attribute is set on the JPALoader or JPAEntityLoader bean.
    • preloadPartition: Specifies the partition at which the map preload is started. If the preload partition is less than zero, or greater than the total number of partitions minus 1, the map preload is not started. The default value is -1, which means the preload does not start by default. This attribute is set on the JPALoader or JPAEntityLoader bean.

    Other than the four JPA parameters to be configured in eXtreme Scale, JPA metadata are used to retrieve the key from the JPA entities. The JPA metadata can be configured as annotation, or as an orm.xml file specified in the persistence.xml file. It is not part of the eXtreme Scale configuration.

  2. Configure XML files for the JPA configuration.

    To configure a JPALoader or JPAEntityLoader, see Plug-ins for communicating with databases.

    Configure a JPATxCallback transaction callback along with the loader configuration. The following example is an ObjectGrid XML descriptor file (objectgrid.xml), that has a JPAEntityLoader and JPATxCallback configured:

    configuring a loader including callback - XML example
    <?xml version="1.0" encoding="UTF-8"?>
    <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd"
    	xmlns="http://ibm.com/ws/objectgrid/config">
        <objectGrids>
          <objectGrid name="JPAEM" entityMetadataXMLFile="jpaEMD.xml">
            <bean id="TransactionCallback" 
               className="com.ibm.websphere.objectgrid.jpa.JPATxCallback">
               <property 
                  name="persistenceUnitName" 
                  type="java.lang.String"  
                  value="employeeEMPU" />
           </bean>
           <backingMap name="Employee" pluginCollectionRef="Employee" />
        </objectGrid>
      </objectGrids>
    
      <backingMapPluginCollections>
        <backingMapPluginCollection id="Employee">
            <bean id="Loader"
              className="com.ibm.websphere.objectgrid.jpa.JPAEntityLoader">
    	      <property 
                   name="entityClassName" 
                   type="java.lang.String"  
                   value="com.ibm.ws.objectgrid.jpa.test.entity.Employee"/>
            </bean>
        </backingMapPluginCollection>
      </backingMapPluginCollections>
    </objectGridConfig>
    

    If you want to configure a JPAPropertyFactory, you have to use a Spring style configuration. The following is an XML configuration file sample,JPAEM_spring.xml which configures a Spring bean to be used for eXtreme Scale configurations.

    configuring a loader including JPA property factory - XML example
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:objectgrid="http://www.ibm.com/schema/objectgrid"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
      <objectgrid:JPAEntityLoader id="jpaLoader" 
    		entityClassName="com.ibm.ws.objectgrid.jpa.test.entity.Employee"/>
      <objectgrid:JPATxCallback id="jpaTxCallback" persistenceUnitName="employeeEMPU" />
    </beans>
    

    The Objectgrid.xml configuration XML file follows. Notice the ObjectGrid name is JPAEM, which matches the ObjectGrid name in the JPAEM_spring.xml Spring configuration file.

    JPAEM loader configuration - XML example
    <?xml version="1.0" encoding="UTF-8"?>
    <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd"
     xmlns="http://ibm.com/ws/objectgrid/config">
      <objectGrids>
        <objectGrid name="JPAEM" entityMetadataXMLFile="jpaEMD.xml">
          <bean id="TransactionCallback" 
                className="{spring}jpaTxCallback"/>
            <backingMap name="Employee" pluginCollectionRef="Employee" 
                        writeBehind="T4"/>
          </objectGrid>
      </objectGrids>
        
      <backingMapPluginCollections>
        <backingMapPluginCollection id="Employee">
           <bean id="Loader" className="{spring}jpaLoader" />
        </backingMapPluginCollection>
      </backingMapPluginCollections>
    </objectGridConfig>
    

    An entity can be annotated with both the JPA annotations and eXtreme Scale entity manager annotations. Each annotation has an XML equivalent that can be used. Thus, eXtreme Scale added the Spring namespace. You can also configure these using the Spring namespace support. For more information, see Spring framework overview.