Java Reference
In-Depth Information
Note
The cache retrieve mode is ignored when calling the
EntityMan-
ager.refresh
method, as calls to
refresh
always result in data
being read from the database, not the cache.
To set the retrieval or store mode when using queries, call the
Query.setHint
or
TypedQuery.setHint
methods, depending on the type of query:
EntityManager em = ...;
CriteriaQuery<Person> cq = ...;
TypedQuery<Person> q = em.createQuery(cq);
q.setHint("javax.persistence.cache.storeMode", "REFRESH");
...
Setting the store or retrieve mode in a query or when calling the
EntityMan-
ager.find
or
EntityManager.refresh
method overrides the setting of the entity
manager.
Controlling the Second-Level Cache Programmatically
The
javax.persistence.Cache
interface defines methods for interacting with the
second-level cache programmatically. The
Cache
interface defines methods to check
whether a particular entity has cached data, to remove a particular entity from the cache,
to remove all instances (and instances of subclasses) of an entity class from the cache, and
to clear the cache of all entity data.
Note
If the second-level cache has been disabled, calls to the
Cache
inter-
face's methods have no effect, except for
contains
, which will al-
ways return
false
.
Checking Whether an Entity's Data Is Cached
Call the
Cache.contains
method to find out whether a given entity is currently in the
second-level cache. The
contains
method returns
true
if the entity's data is cached,
and
false
if the data is not in the cache.