The second-level cache mode ENABLE_SELECTIVE can be used to enable cache for all entities for which Cacheable(true) annotation is specified. All other entities are not cached.
The annotation Cacheable
package javax.persistence;
.....
@Target( { TYPE })
@Retention(RUNTIME)
public @interface Cacheable {
boolean value() default true;
}
Example
persistence.xml
src/main/resources/META-INF/persistence.xml<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="example-unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
<property name="hibernate.cache.region.factory_class"
value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
</properties>
</persistence-unit>
</persistence>
The Entities
@Entity
@Cacheable(true)
public class Employee{
@Id
@GeneratedValue
private long id;
private String name;
private String dept;
.............
}
@Entity
public class Task {
@Id
@GeneratedValue
private long id;
private String description;
.............
}
The main class
public class ExampleMain {
private static EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("example-unit");
public static void main(String[] args) {
try {
persistEntities();
loadEntity();
} finally {
entityManagerFactory.close();
}
}
public static void persistEntities() {
Employee employee = new Employee();
employee.setName("Dawn");
employee.setDept("IT");
Task task = new Task();
task.setDescription("UI Coding");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
em.persist(employee);
em.persist(task);
em.getTransaction().commit();
System.out.println("Employee persisted: " + employee);
System.out.println("Task persisted: " + task);
em.close();
printCacheState();
}
private static void loadEntity() {
EntityManager em = entityManagerFactory.createEntityManager();
Employee employee = em.find(Employee.class, 1L);
System.out.println("Employee loaded: " + employee);
Task task = em.find(Task.class, 2L);
System.out.println("Task loaded: " + task);
em.close();
printCacheState();
}
private static void printCacheState() {
Cache cache = entityManagerFactory.getCache();
boolean contains = cache.contains(Employee.class, 1L);
System.out.printf("Cache#contains() for Employee: %5s%n", contains);
contains = cache.contains(Task.class, 2L);
System.out.printf("Cache#contains() for Task: %10s%n", contains);
}
} Employee persisted: Employee{id=1, name='Dawn', dept='IT'} Task persisted: Task{id=2, description='UI Coding'} Cache#contains() for Employee: true Cache#contains() for Task: false Employee loaded: Employee{id=1, name='Dawn', dept='IT'} Task loaded: Task{id=2, description='UI Coding'} Cache#contains() for Employee: true Cache#contains() for Task: false
Example ProjectDependencies and Technologies Used: - h2 1.4.196: H2 Database Engine.
- hibernate-core 5.2.12.Final: The core O/RM functionality as provided by Hibernate.
Implements javax.persistence:javax.persistence-api version 2.1 - hibernate-ehcache 5.2.12.Final: Integration for Ehcache into Hibernate as a second-level caching service.
- JDK 1.8
- Maven 3.3.9
|