Java Reference
In-Depth Information
EntityManager em = ...;
Cache cache = em.getEntityManagerFactory().getCache();
String personPK = ...;
if (cache.contains(Person.class, personPK)) {
// the data is cached
} else {
// the data is NOT cached
}
Removing an Entity from the Cache
Call one of the
Cache.evict
methods to remove a particular entity or all entities of a
given type from the second-level cache. To remove a particular entity from the cache, call
the
evict
method and pass in the entity class and the primary key of the entity:
EntityManager em = ...;
Cache cache = em.getEntityManagerFactory().getCache();
String personPK = ...;
cache.evict(Person.class, personPK);
To remove all instances of a particular entity class, including subclasses, call the
evict
method and specify the entity class:
EntityManager em = ...;
Cache cache = em.getEntityManagerFactory().getCache();
cache.evict(Person.class);
All instances of the
Person
entity class will be removed from the cache. If the
Person
entity has a subclass,
Student
, calls to the above method will remove all instances of
Student
from the cache as well.
Removing All Data from the Cache
Call the
Cache.evictAll
method to completely clear the second-level cache:
EntityManager em = ...;
Cache cache = em.getEntityManagerFactory().getCache();
cache.evictAll();