Java Reference
In-Depth Information
Cache Store Mode
The cache store mode, set by the
javax.persistence.storeMode
property, con-
trols how data is stored in the cache.
The
storeMode
property can be set to one of the constants defined by the
javax.persistence.CacheStoreMode
enumerated type, either
USE
(the de-
fault),
BYPASS
, or
REFRESH
. When set to
USE
the cache data is created or updated when
data is read from or committed to the database. If data is already in the cache, setting the
store mode to
USE
will not force a refresh when data is read from the database.
When the store mode is set to
BYPASS
, data read from or committed to the database is
not
inserted or updated in the cache. That is, the cache is unchanged.
When the store mode is set to
REFRESH
, the cache data is created or updated when data is
read from or committed to the database, and a refresh is forced on data in the cache upon
database reads.
Setting the Cache Retrieval or Store Mode
To set the cache retrieval or store mode for the persistence context, call the
EntityMan-
ager.setProperty
method with the property name and value pair:
EntityManager em = ...;
em.setProperty("javax.persistence.cache.storeMode", "BYPASS");
To set the cache retrieval or store mode when calling the
EntityManger.find
or
EntityManager.refresh
methods, first create a
Map<String, Object>
in-
stance and add a name/value pair as follows:
EntityManager em = ...;
Map<String, Object> props = new HashMap<String, Object>();
props.put("javax.persistence.cache.retrieveMode", "BYPASS");
String personPK = ...;
Person person = em.find(Person.class, personPK, props);