Hibernate has two levels of caching:
Session.
SessionFactory. It stores the state of an entity instance
in a destructured format, as a tuple of persistent attribute values. The
second-level cache is also used to store cached query result sets.
By nature, a second-level cache tends to undermine the ACID properties of transaction processing in a relational database. A second-level cache is often by far the easiest way to improve the performance of a system, but only at the cost of making it much more difficult to reason about concurrency. And so the cache is a potential source of bugs which are difficult to isolate and reproduce.
Therefore, only entities and collection roles explicitly annotated
jakarta. or org.
are eligible for storage in the second-level cache, and so by default the state
of an entity is always retrieved from the database when requested.
Hibernate segments the second-level cache into named regions, one for
each mapped entity hierarchy or collection role, each with its own policies for
expiry, persistence, and replication, which must be configured externally to
Hibernate. An entity hierarchy or collection role may be explicitly assigned a
region using the org. annotation, but, by
default, the region name is just the name of the entity class or collection role.
The appropriate policies depend on the kind of data an entity represents. For
example, a program might have different caching policies for "reference" data,
for transactional data, and for data used for analytics. Ordinarily, the
implementation of those policies is the responsibility of the
cache provider and is
transparent to code which makes use of a Hibernate Session. At worst,
interaction with the cache may be controlled by specification of an explicit
CacheMode.
Very occasionally, it's necessary or advantageous to control the cache explicitly
via programmatic eviction, using, for example, evictEntityData(Class) to
evict a whole cache region, or evictEntityData(Class, Object), to evict
a single item.
If multiple entities or roles are mapped to the same cache region, they share
policies and even the same FIFO-type expiry queue (if any). This sounds useful,
but comes with the downside that evictEntityData(Class) for any one of
the entities evicts all entities mapped to the same region. It's
therefore much more common to have a distinct region for each entity and role.
None of the operations of this interface respect any isolation or transactional semantics associated with the underlying caches. In particular, eviction via the methods of this interface causes an immediate "hard" removal outside any current transaction and/or locking scheme.
The org. annotation also specifies a
org., a policy governing
access to the second-level cache by concurrent transactions. Either:
It's important to always explicitly specify an appropriate policy, taking into account the expected patterns of data access, most importantly, the frequency of updates.
Query result sets may also be stored in the second-level cache. A query is made
eligible for caching by calling
org., and may be
assigned to a region of the second-level cache by calling
org.. It's very
important to understand that any entity instance in a query result set is cached
by its id. If the entity itself is not cacheable, or if the instance is not available in the second-level cache at the
time a result set is retrieved from the cache, then the state of the entity must
be read from the database. This negates the benefits of caching the result
set. It's therefore very important to carefully "match" the caching policies
of a query and the entities it returns.
Hibernate does not itself contain a high-quality implementation of a second-level
cache backend with expiry, persistence, and replication, and depends on a plug-in
implementation of org. to integrate a
backend storage mechanism. Therefore, the second-level cache is completely disabled
by default, unless hibernate.cache.region.factory_class
is explicitly specified. For convenience, the second-level cache may also be enabled
or disabled using hibernate.cache.use_second_level_cache.
org.hibernate.annotations.Cache , org.hibernate.annotations.CacheConcurrencyStrategy , org.hibernate.cfg.AvailableSettings#CACHE_REGION_FACTORY , org.hibernate.cfg.AvailableSettings#USE_SECOND_LEVEL_CACHE
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: True if the underlying cache contains corresponding data; false otherwise.The name of the collection role in the form
role, Object package.OwnerEntityName.collectionPropertyNameThe identifier of the owning entity ownerIdentifier)Determine whether the cache contains an item for the collection with the given role and given identifier. |
| public boolean | Returns: True if the underlying cache contains corresponding data; false otherwise.The entity type entityClass, Object The entity identifier identifier)Determine whether the cache contains an item for the entity of the given type, and with the given identifier. |
| public boolean | Returns: True if the underlying cache contains corresponding data; false otherwise.The entity name entityName, Object The entity identifier identifier)Determine whether the cache contains an item for the entity of the type with the given name, and with the given identifier. |
| public boolean | Returns: True if the underlying cache contains corresponding data; false otherwise.The name of a cache region to which some query is assigned regionName)Determine whether the given region name contains cached query results. |
| public default void | |
| public default void | |
| public void | evictCollectionData(String
The name of the collection role in the form
role, Object package.OwnerEntityName.collectionPropertyNameThe identifier of the owning entity ownerIdentifier)Evict the cached item for the collection with the given role and given identifier, if there is any such item in the cache. |
| public void | evictCollectionData(String
The name of the collection role in the form
role)package.OwnerEntityName.collectionPropertyNameEvict all cached data from the cache region to which the given collection role is assigned. |
| public void | evictCollectionData()
Evict all cache data from every cache region to which some collection role is assigned. |
| public void | |
| public void | evictEntityData(Class<?>
The entity type entityClass, Object The entity identifier identifier)Evicts the cached item for the entity of the given type, and with the given identifier, if there is any such item in the cache. |
| public void | evictEntityData(String
The entity name entityName, Object The entity identifier identifier)Evict the cached item for the entity of the type with the given name, and with the given identifier, if there is any such item in the cache. |
| public void | evictEntityData(Class<?>
The entity type entityClass)Evict all cached data from the cache region to which the given entity type is assigned. |
| public void | evictEntityData(String
The entity name entityName)Evict all cached data from the cache region to which the given named entity type is assigned. |
| public void | evictEntityData()
Evict all cached data from every cache region to which any entity type is assigned. |
| public void | evictNaturalIdData(Class<?>
The entity type entityClass)Evict all cached natural id mappings for the given entity type. |
| public void | evictNaturalIdData(String
The entity name entityName)Evict all cached natural id mappings for the entity type with the given name. |
| public void | |
| public void | evictQueryRegion(String
The cache name associated to the queries being cached. regionName)Evict all cached query results from the region with the given name. |
| public void | |
| public void | |
| public SessionFactory |
| containsCollection | back to summary |
|---|---|
| public boolean containsCollection(String role, Object ownerIdentifier) Determine whether the cache contains an item for the collection with the given role and given identifier. | |
| containsEntity | back to summary |
|---|---|
| public boolean containsEntity(Class<?> entityClass, Object identifier) Determine whether the cache contains an item for the entity of the given type, and with the given identifier. | |
| containsEntity | back to summary |
|---|---|
| public boolean containsEntity(String entityName, Object identifier) Determine whether the cache contains an item for the entity of the type with the given name, and with the given identifier. | |
| containsQuery | back to summary |
|---|---|
| public boolean containsQuery(String regionName) Determine whether the given region name contains cached query results.
| |
| evictAll | back to summary |
|---|---|
| public default void evictAll() Implements jakarta. Doc from jakarta. Clear the cache. API Note This operation only affects cached data for entities, in keeping
with the intent of the JPA specification, which only defines caching for
entity data. To evict all data from every cache region, including cached
collections, natural-id mappings, and cached query results, use
| |
| evictAllRegions | back to summary |
|---|---|
| public default void evictAllRegions() Evict all cached data from every cache region. | |
| evictCollectionData | back to summary |
|---|---|
| public void evictCollectionData(String role, Object ownerIdentifier) Evict the cached item for the collection with the given role and given identifier, if there is any such item in the cache. | |
| evictCollectionData | back to summary |
|---|---|
| public void evictCollectionData(String role) Evict all cached data from the cache region to which the given collection role is assigned.
| |
| evictCollectionData | back to summary |
|---|---|
| public void evictCollectionData() Evict all cache data from every cache region to which some collection role is assigned.
| |
| evictDefaultQueryRegion | back to summary |
|---|---|
| public void evictDefaultQueryRegion() Evict all cached query results from the default region. | |
| evictEntityData | back to summary |
|---|---|
| public void evictEntityData(Class<?> entityClass, Object identifier) Evicts the cached item for the entity of the given type, and with the given identifier, if there is any such item in the cache. | |
| evictEntityData | back to summary |
|---|---|
| public void evictEntityData(String entityName, Object identifier) Evict the cached item for the entity of the type with the given name, and with the given identifier, if there is any such item in the cache. | |
| evictEntityData | back to summary |
|---|---|
| public void evictEntityData(Class<?> entityClass) Evict all cached data from the cache region to which the given entity type is assigned. Thus, every cached item for the given entity type will be evicted, along with any cached items for any other entity type assigned to the same cache region.
| |
| evictEntityData | back to summary |
|---|---|
| public void evictEntityData(String entityName) Evict all cached data from the cache region to which the given named entity type is assigned. Thus, every cached item for the given entity type will be evicted, along with any cached items for any other entity type assigned to the same cache region.
| |
| evictEntityData | back to summary |
|---|---|
| public void evictEntityData() Evict all cached data from every cache region to which any entity type is assigned.
| |
| evictNaturalIdData | back to summary |
|---|---|
| public void evictNaturalIdData(Class<?> entityClass) Evict all cached natural id mappings for the given entity type.
| |
| evictNaturalIdData | back to summary |
|---|---|
| public void evictNaturalIdData(String entityName) Evict all cached natural id mappings for the entity type with the given name.
| |
| evictNaturalIdData | back to summary |
|---|---|
| public void evictNaturalIdData() Evict all cached natural id mappings for every entity type.
| |
| evictQueryRegion | back to summary |
|---|---|
| public void evictQueryRegion(String regionName) Evict all cached query results from the region with the given name.
| |
| evictQueryRegions | back to summary |
|---|---|
| public void evictQueryRegions() Evict all cached query results from every region. | |
| evictRegion | back to summary |
|---|---|
| public void evictRegion(String regionName) Evict all cached data from the named cache region.
| |
| getSessionFactory | back to summary |
|---|---|
| public SessionFactory getSessionFactory() The
| |