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.

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.

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.

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.

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.

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 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:

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.

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.

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.

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.

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.

Scripting on this page enhances content navigation, but does not change the content in any way.