Persistences Caching - JPA

The basics

If you want to store your value
in a
, it is really simple.

The only thing you need is a implementation of an entity class which implements CachedJPAEntity<K,V> and getting an instance of "EntityManagerFactory".
The rest is provided by the default implementations

An example using the
(see below) where both key and value is String.
LRUCachingMap<String, String> map = 
	new DefaultLRUCachingMap<String, String>(
		new DefaultJPAEntityValueManager<String,String>(
			new DefaultCachedJPAEntityRepository<String,String>(

Object persistence

If you want to persist an Class instance like a "Book" or "Movie" instead of "String", this is also quiet simple, but there is some few basic concepts to follow.
the class must implement "Serializable", and the value column must be declared as:

private Book value;


There is also a factory class to all this -

So the code above can in reality be done like.
DefaultCachedJPALRUCachingMap<String,String> cache = LRUCachingMapFactory.createJPACache(DefaultLRUCachingMapConfiguration.ONE_DAY(),MyCachedJPAEntity.class,entityManagerFactory,PersistencesRestoreMethod.DELETE_ALL);


It will store this in a table, columns names defined in MyCachedJPAEntity (Remember to think about length):
    //(HSQLDB sql)
    create table MyCacheTable (
        mykey varchar(1000) not null,     //Primary key
        myexpiretime timestamp not null,
        mystoretime timestamp not null,
        myvalue varchar(4000) not null,
        primary key (mykey)

Example - MyCachedJPAEntity

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

public class MyCachedJPAEntity implements CachedJPAEntity<String,String> {

	private static final long serialVersionUID = -5695149079796743961L;

	private String key;
	private String value;
	private Date storeDateTime;
	private Date expireDateTime;
	public MyCachedJPAEntity() {
	public String getKey() {
		return key;

	public String getValue() {
		return value;

	public Date getStoredDateTime() {
		return storeDateTime;

	public Date getExpireDateTime() {	
		return expireDateTime;
	public void setExpireDateTime(Date expireDateTime) {
	public void setKey(String key) {
		this.key = key;
	public void setStoredDateTime(Date storeDateTime) {
		this.storeDateTime = storeDateTime;
	public void setValue(String value) {
		this.value = value;


Example - persistences.xml (src/test/resources/META-INF/persistence.xml)

<?xml version="1.0" encoding="UTF-8"?>
    xmlns:xsi="" xsi:schemaLocation="" version="2.0">
    <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.use_sql_comments" value="true" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa" />
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="" value="create-drop" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />