Fusion Middleware Documentation
Advanced Search


Integrating Oracle Coherence
Close Window

Table of Contents

Show All | Collapse

2 Integrating JPA Using the Coherence API

This chapter describes how to use the Coherence API with caches backed by TopLink Grid to access relational data. It also describes how to use the native, entity-based Coherence implementations of the cache store and cache loader to access relational data. These implementations use JPA to load and store objects to the database.

Note:

Only resource-local and bootstrapped entity managers can be used with Coherence API and JPA. Container-managed entity managers and those that use Java Transaction Architecture (JTA) transactions are not currently supported.

This chapter contains the following sections:

2.1 Using TopLink Grid with Coherence Client Applications

This section describes how to use the Coherence API with caches backed by TopLink Grid to access relational data. Access to the relational data is provided with JPA cache loader and cache store interfaces which have been optimized for EclipseLink JPA.

In this traditional Coherence approach, TopLink Grid provides the CacheLoader and CacheStore implementations in the oracle.eclipselink.coherence.standalone package that are optimized for EclipseLink JPA.

Figure 2-1 illustrates the relationship between the client application (which employs Coherence APIs), the Coherence cache, TopLink Grid, and the database.

Figure 2-1 Coherence with TopLink Grid Approach

Traditional Coherence approach.
Description of "Figure 2-1 Coherence with TopLink Grid Approach"

2.1.1 API for Coherence with TopLink Grid Configurations

TopLink Grid uses the standard JPA run-time configuration file persistence.xml and the JPA mapping file orm.xml. The Coherence cache configuration file coherence-cache-config.xml must be specified to override the default Coherence settings and to define the cache store caching scheme.

The TopLink Grid cache store and cache loader implementations are shipped in the toplink-grid.jar file. The JAR file in installed with the Coherence product in the ...\oracle_common\modules\oracle.toplink_12.1.2 folder.

The TopLink Grid cache store and cache loader classes which are optimized for EclipseLink JPA and designed for use by Coherence applications, are in the oracle.eclispelink.coherence.standalone package. Table 2-1 describes these classes.

Table 2-1 TopLink Grid Classes to build Coherence with TopLink Grid Applications

Class Name Description

EclipseLinkJPACacheLoader

Provides JPA-aware versions of the Coherence CacheLoader class.

EclipseLinkJPACacheStore

Provides JPA-aware versions of the Coherence CacheStore class.


2.1.2 Sample Cache Configuration File for Coherence with TopLink Grid

In the cache configuration file (coherence-cache-config.xml), define the cache as illustrated in Example 2-1. For TopLink Grid, you have to define only two parameters:

  • The name of the cache for the entity being stored. Unless explicitly overridden in JPA this is the entity name that, by default, is the unqualified name of the entity class. In Example 2-1, the name of the cache is Employee. You can use the built-in Coherence macro {cache-name} to supply the name of the cache that is constructing and using the cache store.

  • The name of the persistence unit containing the entity being stored. In Example 2-1, employee-pu is a persistence unit defined in the META-INF/persistence.xml file that includes the Employee entity.

    To define more entity caches, add additional <cache-mapping> elements.

Example 2-1 Configuring the Cache for Coherence with TopLink Grid

<cache-config>
  <caching-scheme-mapping>
    <cache-mapping>
      <cache-name>Employee</cache-name>
      <scheme-name>distributed-eclipselink</scheme-name>
    </caching-scheme-mapping>
  </caching-scheme-mapping>
  <caching-schemes>
    <distributed-scheme>
      <scheme-name>distributed-eclipselink</scheme-name>
      <service-name>EclipseLinkJPA</service-name>
      <backing-map-scheme>
        <read-write-backing-map-scheme>
          <internal-cache-scheme>
            <local-scheme />
          </internal-cache-scheme>
          <!-- 
            Define the cache scheme. 
          -->
          <cachestore-scheme>
            <class-scheme>
            <!-- 
              Because the client code is using Coherence API, use the "standalone" version of the cache loader. 
            -->
              <class-name>oracle.eclipselink.coherence.standalone.EclipseLinkJPACacheStore</class-name>
              <init-params>

           <!-- This parameter is the name of the cache containing the entity. -->
                <init-param>
                  <param-type>java.lang.String</param-type>
                  <param-value>{cache-name}</param-value>
                </init-param>

            <!-- This parameter is the persistence unit name. -->
                <init-param>
                  <param-type>java.lang.String</param-type>
                  <param-value>employee-pu</param-value>
                </init-param>
              </init-params>
            </class-scheme>
          </cachestore-scheme>
        </read-write-backing-map-scheme>
      </backing-map-scheme>
      <autostart>true</autostart>
    </distributed-scheme>
  </caching-schemes></cache-config>

2.1.3 Sample Project for Using Coherence with TopLink Grid

"Using JPA with Coherence" in the Oracle Fusion Middleware Tutorial for Oracle Coherence provides a sample project that uses the TopLink Grid cache store and cache loader classes which are optimized for EclipseLink JPA and designed for use by Coherence applications. These classes can be found in the oracle.eclispelink.coherence.standalone package.

The project uses the Oracle Express Database and the Eclipse IDE to configure a project for JPA, create the JPA persistence unit and entities, edit the persistence.xml file, create a cache configuration file for JPA, automatically generate JPA objects for a database table, and create a class to interact with the data objects.

2.2 Using Third Party JPA Providers

Oracle Coherence provides its own implementations of the CacheLoader and CacheStore classes which can be used with JPA. The JpaCacheLoader and JpaCacheStore classes do not have to use EclipseLink JPA—they can use any JPA implementation to load and store entities to and from a data store. The entities must be mapped to the data store and a JPA persistence unit configuration must exist. A JPA persistence unit is defined as a logical grouping of user-defined entity classes that can be persisted and their settings.

Coherence also provides a default cache configuration file named coherence-cache-config.xml. The JPA run-time configuration file, persistence.xml, and the default JPA Object-Relational mapping file, orm.xml, are typically provided by the JPA implementation.

2.2.1 API for Native Coherence JPA CacheStore and CacheLoader

The JpaCacheLoader and JpaCacheStore classes can be found in the coherence-jpa.jar file, which is installed in the ...\coherence\lib folder in the Coherence installation. The CacheLoader and CacheStore interfaces can be found in the coherence.jar file, which is also installed in the ...\coherence\lib folder.

Table 2-2 describes the default JPA implementations provided by Coherence.

Table 2-2 JPA-Related CacheStore and CacheLoader API Included with Coherence

Class Name Description

com.tangosol.net.cache.CacheLoader

A JCache cache loader.

com.tangosol.net.cache.CacheStore

A JCache cache store. The CacheStore interface extends CacheLoader.

com.tangosol.coherence.jpa.JpaCacheLoader

The JPA implementation of the Coherence CacheLoader interface. Use this class as a load-only implementation. It can use any JPA implementation to load entities from a data store. The entities must be mapped to the data store and a JPA persistence unit configuration must exist.

Use the JpaCacheStore class for a full load and store implementation.

com.tangosol.coherence.jpa.JpaCacheStore

The JPA implementation of the Coherence CacheStore interface. Use this class as a full load and store implementation. It can use any JPA implementation to load and store entities to and from a data store. The entities must be mapped to the data store and a JPA persistence unit configuration must exist.

Note: The persistence unit is assumed to be set to use RESOURCE_LOCAL transactions.


2.2.2 Steps to Use a Third Party JPA Provider and Native Coherence JPA API

To use a third party JPA provider and the native Coherence JPA API to load and store objects to the database:

  1. Obtain a JPA Provider Implementation. The provider implementation allows you to map, query, and store Java objects to a database.

  2. Configure a Coherence JPA Cache Store. The JPA cache store configuration maps database entities to Java objects.

2.2.2.1 Obtain a JPA Provider Implementation

A JPA provider allows you to work directly with Java objects, rather then with SQL statements. You can map, store, update and retrieve data, and the provider will perform the translation between database entities and Java objects.

The Coherence JPA cache store and cache loader work with any JPA-compliant implementation. Oracle recommends using EclipseLink JPA, the reference implementation for the JPA 2.0 specification. Oracle TopLink and TopLink Grid for Coherence integration include EclipseLink as their JPA implementations.

The TopLink Grid and EclipseLink JAR files (toplink-grid.jar and eclipselink.jar) are included in the Coherence installation and can be found in the ...\oracle_common\modules\oracle.toplink_12.1.2 folder.

2.2.2.2 Configure a Coherence JPA Cache Store

JPA is a standard API for mapping, querying, and storing Java objects to a database. The characteristics of the different JPA implementations can differ, however, when it comes to caching, threading, and overall performance. EclipseLink provides a high-performance JPA implementation with many advanced features.

Coherence provides a default entity-based cache store implementation, JpaCacheStore, and a corresponding cache loader, JpaCacheLoader. You can find additional information in the Javadoc for these classes.

To configure a Coherence JpaCacheStore:

  1. Map the Persistent Classes

  2. Configure JPA

  3. Configure a Coherence Cache for JPA

  4. Configure the Persistence Unit

2.2.2.2.1 Map the Persistent Classes

Map the entity classes to the database. This will allow you to load and store objects through the JPA cache store. JPA mappings are standard, and can be specified in the same way for all JPA providers.

You can map entities either by annotating the entity classes or by adding an orm.xml or other XML mapping file. See the JPA provider documentation for more information about how to map JPA entities.

2.2.2.2.2 Configure JPA

Edit the persistence.xml file to create the JPA configuration. This file contains the properties that dictate run-time operation.

Set the transaction type to RESOURCE_LOCAL and provide the required JDBC properties for your JPA provider (such as driver, url, user, and password) with the appropriate values for connecting and logging into your database. List the classes that are mapped using JPA annotations in <class> elements. Example 2-2 illustrates a sample persistence.xml file with the typical properties that you can set.

Example 2-2 Sample persistence.xml File for JPA

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="EmpUnit" transaction-type="RESOURCE_LOCAL"> 
    <provider>
        org.eclipse.persistence.jpa.PersistenceProvider
    </provider>
    <class>com.oracle.coherence.handson.Employee</class>
    <properties>
        <property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
        <property name="eclipselink.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
        <property name="eclipselink.jdbc.user" value="scott"/>
        <property name="eclipselink.jdbc.password" value="tiger"/>
    </properties>
</persistence-unit>
</persistence>
2.2.2.2.3 Configure a Coherence Cache for JPA

Create a coherence-cache-config.xml file to override the default Coherence settings and define the JpaCacheStore caching scheme. The caching scheme should include a <cachestore-scheme> element that lists the JpaCacheStore class and includes the following parameters.

  • The entity name of the entity being stored. Unless it is explicitly overridden in JPA, this is the unqualified name of the entity class. Example 2-3 uses the built-in Coherence macro {cache-name} that translates to the name of the cache that is constructing and using the cache store. This works because a separate cache must be used for each type of persistent entity and Coherence ensures that the name of each cache is set to the name of the entity that is being stored in it.

  • The fully qualified name of the entity class. If the classes are all in the same package and use the default JPA entity names, then you can again use the {cache-name} macro for the part that is variable across the different entity types. In this way, the same caching scheme can be used for all of the entities that are cached within the same persistence unit.

  • The persistence unit name. This should be the same as the name specified in the persistence.xml file.

The various named caches are then directed to use the JPA caching scheme. Example 2-3 is a sample coherence-cache-config.xml file that defines a NamedCache class named Employee that caches instances of the Employee class. To define additional entity caches for more classes, add more <cache-mapping> elements to the file.

Example 2-3 Assigning Named Caches to a JPA Caching Scheme

<cache-config>
  <caching-scheme-mapping>
    <cache-mapping>
      <!-- Set the name of the cache to be the entity name.  -->
      <cache-name>Employee</cache-name>
      <!-- Configure this cache to use the following defined scheme. -->
      <scheme-name>jpa-distributed</scheme-name>
    </cache-mapping>
  </caching-scheme-mapping>
  <caching-schemes>
    <distributed-scheme>
      <scheme-name>jpa-distributed</scheme-name>
      <service-name>JpaDistributedCache</service-name>
      <backing-map-scheme>
        <read-write-backing-map-scheme>
          <internal-cache-scheme>
            <local-scheme/>
          </internal-cache-scheme>
          <!- Define the cache scheme. -->
          <cachestore-scheme>
            <class-scheme>
              <class-name>
                com.tangosol.coherence.jpa.JpaCacheStore
              </class-name>
              <init-params>

                <!-- This param is the entity name. -->
                <init-param>
                  <param-type>java.lang.String</param-type>
                  <param-value>{cache-name}</param-value>
                </init-param>

                <!-- This param is the fully qualified entity class. -->
                <init-param>
                  <param-type>java.lang.String</param-type>
                  <param-value>com.acme.{cache-name}</param-value>
                </init-param>

                <!-- This param should match the value of the -->
                <!-- persistence unit name in persistence.xml. -->
                <init-param>
                  <param-type>java.lang.String</param-type>
                  <param-value>EmpUnit</param-value>
                </init-param>
              </init-params>
            </class-scheme>
          </cachestore-scheme>
        </read-write-backing-map-scheme>
      </backing-map-scheme>
    </distributed-scheme>
  </caching-schemes>
</cache-config>
2.2.2.2.4 Configure the Persistence Unit

When using the JpaCacheStore class, configure the persistence unit to ensure that no changes are made to entities when they are inserted or updated. Any changes made to entities by the JPA provider are not reflected in the Coherence cache. This means that the entity in the cache will not match the database contents. In particular, entities should not use ID generation, for example, @GeneratedValue, to obtain an ID. IDs should be assigned in application code before an object is put into Coherence. The ID is typically the key under which the entity is stored in Coherence.

Optimistic locking (for example, @Version) should not be used because it might lead to the failure of a database transaction commit transaction. See Caching Data Sources and Sample CacheStore in Oracle Fusion Middleware Developing Applications with Oracle Coherence for more information about how a cache store works, and how to set up your database schema.

When using either the JpaCacheStore or JpaCacheLoader class, L2 ("shared") caching should be disabled in your persistence unit. See the documentation for your provider. In Oracle TopLink, this can be specified on an individual entity with @Cache(shared=false) or as the default in the persistence.xml file with the following property:

<property name="eclipselink.cache.shared.default" value="false"/>

When using EclipseLink with TopLink Grid, the TopLink Grid implementations will automatically disable L2 caching, optimistic lock checking, and versioning. Essentially, TopLink Grid implementations understand the cache store context in which the persistence unit is being deployed and adjust the configuration accordingly.