3.11.1.2.1. Using the JPA standard Cache interface

You may obtain the jakarta.persistence.Cache through the EntityManagerFactory.getCache() method.

Example 146. Accessing the Cache
import jakarta.persistence.Cache;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
. . .
EntityManagerFactory emf =
    Persistence.createEntityManagerFactory("myPersistenceUnit");
Cache cache = emf.getCache();
. . .
Example 147. Using the jakarta.persistence.Cache interface
// Check whether the cache contains an entity with a provided ID
Cache cache = emf.getCache();
boolean contains = cache.contains(MyEntity.class, entityID);

// evict a specific entity from the cache
cache.evict(MyEntity.class, entityID);

// evict all instances of an entity class from the cache
cache.evict(AnotherEntity.class);

// evict everything from the cache
cache.evictAll();