1. Introduction
The Spring Data Coherence module provides integration with Coherence data grids. Key functional areas of Spring Data Coherence are a POJO centric model for interacting with a Coherence data grid and easily writing a Repository style data access layer.
2. Features
-
Spring configuration support using Java-based @Configuration classes.
-
Automatic implementation of Repository interfaces
-
Rich query and event features from Coherence
2.1. Functionality Not Supported in the M1 Release
-
Spring Data finder queries that use paging
-
Interface-based projection
-
Exception translation into Spring’s portable Data Access Exception hierarchy
These will be supported in the next milestone release. Another feature for the next milestone release will be a native asynchronous repository.
3. Getting Started
<dependency>
<groupId>com.oracle.coherence.spring</groupId>
<artifactId>coherence-spring-data</artifactId>
<version>3.0.0-M1</version>
</dependency>
implementation("com.oracle.coherence.spring:coherence-spring-data:3.0.0-M1")
4. Defining Repositories
Before proceeding, please be familiar with the Spring Data Commons documentation, as this section will assume some familiarity with Spring Data.
Simple repositories such as the following will, of course, work as expected:
public interface PersonRepository extends CrudRepository<String, Person> {
// ...
}
However, it is recommended to extend the CoherenceRepository interface, to fully utilize the features Coherence for Spring Data has to offer such as:
-
Powerful projection features
-
Flexible in-place entity updates
-
First-class data aggregation support
-
Stream API support
-
Event listener support
-
Declarative acceleration and index creation
Please also see the Coherence Repository documentation for more details on these features.
import com.oracle.coherence.spring.data.repository.CoherenceRespository;
// ...
public interface PersonRepository extends CoherenceRepository<String, Person> {
// ...
}
4.1. Identifying the Coherence NamedMap
The Coherence NamedMap
that will be used by the Repository implementation
will be based on the type name in the Repository class assuming the Repository
name follows the format of [Type]Repository (e.g., PersonRepository will use a NamedMap called person
).
If this is not desired, the name may instead be passed by the @CoherenceRepository
annotation. For example:
import com.oracle.coherence.spring.data.config.CoherenceMap;
import com.oracle.coherence.spring.data.repository.CoherenceRespository;
// ...
@CoherenceMap("people")
public interface PersonRepository extends CoherenceRepository<String, Person> {
// ...
}
5. Mapping Entities
As Coherence is, at its core, a key-value store, mapping Entities for use with a Coherence
Repository is relatively simple. It requires using the javax.persistence.Entity
annotation
to define the domain type that will be persisted, and the javax.persistence.Id
annotation to denote the entity’s id.
For example:
@javax.persistence.Entity
public class Person implements Serializable {
@javax.persistence.Id
protected String id;
// ---- person functionality ----
}
Any other javax.persistence
annotations will not be considered by the runtime.
6. Using the Repository
In order to enable Coherence-based Repositories, you must use the @EnableCoherenceRepositories annotation. A simple configuration example would be:
import com.oracle.coherence.spring.configuration.annotation.EnableCoherence;
import com.oracle.coherence.spring.configuration.data.config.EnableCoherenceRepositories;
// ...
@Configuration
@EnableCoherence
@EnableCoherenceRepositories
public static class Config {
}
Similarly to other Spring Data implementations, the @EnableCoherenceRepositories
annotation
offers several configuration options to configure how Spring will search for repositories.
Please see the API docs for details.
6.1. Finder Queries
One of the benefits of Spring Data is the ability to define queries on the Repository interface using Spring Data’s finder query syntax. For example:
import com.oracle.coherence.spring.data.repository.CoherenceRespository;
// ...
public interface BookRepository extends CoherenceRepository<Book, UUID> {
List<Book> findByAuthor(Author author);
// other finders
}
As of the M1 release, however, finder queries using pagination are not supported. This functionality is targeted for the next milestone release.