1 Using JPA with Coherence
CacheStore
and CacheLoader
interfaces that use the Java Persistence API (JPA) to load and store objects to a database. Before using JPA with Coherence, you should be familiar with the CacheStore
and CacheLoader
interfaces. These interfaces are used to cache data sources. See Caching Data Sources.
Note:
Only resource-local and bootstrapped entity managers can be used with Coherence and JPA. Container-managed entity managers and those that use Java Transaction Architecture (JTA) transactions are not currently supported.
This chapter includes the following sections:
- Overview of the JPA CacheStore and CacheLoader Implementations
Oracle Coherence provides two implementations of theCacheStore
andCacheLoader
interfaces which can be used with JPA: a generic JPA implementation and an EclipseLink-specific implementation. - Obtain a JPA Provider Implementation
A JPA provider allows you to work directly with Java objects, rather then with SQL statements. You map, store, update and retrieve data, and the provider performs the translation between database entities and Java objects. - Configure a Coherence JPA Cache Store
Using JPA with Coherence requires configuring persistence properties and defining a cache that uses theJpaCacheStore
implementation.
Overview of the JPA CacheStore and CacheLoader Implementations
CacheStore
and CacheLoader
interfaces which can be used with JPA: a generic JPA implementation and an EclipseLink-specific implementation. For both implementations, 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. The JPA run-time configuration file, persistence.xml
, and the default JPA Object-Relational mapping file, orm.xml
, are typically provided as part of a JPA solution.
Table 1-1 describes the JPA implementations provided by Coherence.
Table 1-1 JPA-Related CacheStore and CacheLoader API Included with Coherence
Class Name | Location | Description |
---|---|---|
|
COHERENCE_HOME\lib\coherence-jpa.jar |
A JPA implementation of the Coherence Note: The persistence unit is assumed to be set to use |
|
A JPA implementation of the Coherence Use the |
|
EclipseLinkJPACacheStore |
ORACLE_HOME\oracle_common\modules\oracle.toplink_version\toplink-grid.jar |
An EclipseLink specific JPA implementation of the Coherence Note: To use this implementation, make sure no cache interceptors or query redirectors from the EclipseLink-Coherence integration are set within the persistence unit for the specific class. |
EclipseLinkJPACacheLoader |
An EclipseLink specific JPA implementation of the Coherence Note: To use this implementation, make sure no cache interceptors or query redirectors from the EclipseLink-Coherence integration are set within the persistence unit for the specific class. |
Parent topic: Using JPA with Coherence
Obtain a JPA Provider Implementation
A JPA provider allows you to work directly with Java objects, rather then with SQL statements. You map, store, update and retrieve data, and the provider performs the translation between database entities and Java objects.
Oracle recommends using EclipseLink JPA– the reference implementation for the JPA 2.0 specification and also the JPA provider used in Oracle TopLink. EclipseLink provides a high-performance JPA implementation with many advanced features for caching, threading, and overall performance.
The EclipseLink JAR files (eclipselink.jar
) is included in the Coherence installation and can be found in the ORACLE_HOME\oracle_common\modules\oracle.toplink_version
folder.
Parent topic: Using JPA with Coherence
Configure a Coherence JPA Cache Store
Using JPA with Coherence requires configuring persistence properties and defining a cache that uses the JpaCacheStore
implementation.
This section includes the following topics:
- Map the Persistent Classes
- Configure JPA
- Configure a Coherence Cache for JPA
- Configure the Persistence Unit
Parent topic: Using JPA with Coherence
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.
Parent topic: Configure a Coherence JPA Cache Store
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 1-1 illustrates a sample persistence.xml
file with the typical properties that you can set.
Example 1-1 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>
Parent topic: Configure a Coherence JPA Cache Store
Configure a Coherence Cache for JPA
Create a coherence-cache-config.xml
file to override the default Coherence settings and define a caching scheme. The caching scheme includes a <cachestore-scheme>
element that lists the JPA implementation 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 1-2 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 1-2 is a sample coherence-cache-config.xml
file that defines a cache named Employee
that caches instances of the Employee
class. The cache is configured to use the JpaCacheStore
implementation. To define additional entity caches for more classes, add more <cache-mapping>
elements to the file.
Example 1-2 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>
Parent topic: Configure a Coherence JPA Cache Store
Configure the Persistence Unit
When using a JPA cache store or loader implementation, 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.
When using a JPA cache store or loader implementation, L2 (shared) caching should be disabled in your persistence unit. See the documentation for your provider. In EclipseLink, 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"/>
Parent topic: Configure a Coherence JPA Cache Store