Top Description Methods
org.hibernate

public Interface Cache

extends Cache
Known Direct Subinterfaces
org.hibernate.cache.spi.CacheImplementor

An API for directly querying and managing the second-level cache.

Hibernate has two levels of caching:

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.persistence.Cacheable or org.hibernate.annotations.Cache 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.hibernate.annotations.Cache 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.hibernate.annotations.Cache annotation also specifies a org.hibernate.annotations.CacheConcurrencyStrategy, 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.hibernate.query.SelectionQuery#setCacheable(boolean), and may be assigned to a region of the second-level cache by calling org.hibernate.query.SelectionQuery#setCacheRegion(String). 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.hibernate.cache.spi.RegionFactory 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.

Author
Steve Ebersole
See Also
org.hibernate.annotations.Cache, org.hibernate.annotations.CacheConcurrencyStrategy, org.hibernate.cfg.AvailableSettings#CACHE_REGION_FACTORY, org.hibernate.cfg.AvailableSettings#USE_SECOND_LEVEL_CACHE

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

True if the underlying cache contains corresponding data; false otherwise.
containsCollection
(String
The name of the collection role in the form package.OwnerEntityName.collectionPropertyName
role
,
Object
The 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.
containsEntity
(Class<?>
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.
containsEntity
(String
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.
containsQuery
(String
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
evictAll()

Implements jakarta.persistence.Cache.evictAll.

Clear the cache.
public default void
evictAllRegions()

Evict all cached data from every cache region.

public void
evictCollectionData(String
The name of the collection role in the form package.OwnerEntityName.collectionPropertyName
role
,
Object
The 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 package.OwnerEntityName.collectionPropertyName
role
)

Evict 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
evictDefaultQueryRegion()

Evict all cached query results from the default region.

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
evictNaturalIdData()

Evict all cached natural id mappings for every entity type.

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
evictQueryRegions()

Evict all cached query results from every region.

public void
evictRegion(String regionName)

Evict all cached data from the named cache region.

public SessionFactory

Returns:

The SessionFactory
getSessionFactory
()

The SessionFactory to which this Cache belongs.

Inherited from jakarta.persistence.Cache:
containsevictevictunwrap

Method Detail

containsCollectionback 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.

Parameters
role:String

The name of the collection role in the form package.OwnerEntityName.collectionPropertyName

ownerIdentifier:Object

The identifier of the owning entity

Returns:boolean

True if the underlying cache contains corresponding data; false otherwise.

containsEntityback 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.

Parameters
entityClass:Class<?>

The entity type

identifier:Object

The entity identifier

Returns:boolean

True if the underlying cache contains corresponding data; false otherwise.

containsEntityback 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.

Parameters
entityName:String

The entity name

identifier:Object

The entity identifier

Returns:boolean

True if the underlying cache contains corresponding data; false otherwise.

containsQueryback to summary
public boolean containsQuery(String regionName)

Determine whether the given region name contains cached query results.

Parameters
regionName:String

The name of a cache region to which some query is assigned

Returns:boolean

True if the underlying cache contains corresponding data; false otherwise.

evictAllback to summary
public default void evictAll()

Implements jakarta.persistence.Cache.evictAll.

Doc from jakarta.persistence.Cache.evictAll.

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() instead.

Annotations
@Override
evictAllRegionsback to summary
public default void evictAllRegions()

Evict all cached data from every cache region.

evictCollectionDataback 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.

Parameters
role:String

The name of the collection role in the form package.OwnerEntityName.collectionPropertyName

ownerIdentifier:Object

The identifier of the owning entity

Since
5.3
evictCollectionDataback to summary
public void evictCollectionData(String role)

Evict all cached data from the cache region to which the given collection role is assigned.

Parameters
role:String

The name of the collection role in the form package.OwnerEntityName.collectionPropertyName

Since
5.3
evictCollectionDataback to summary
public void evictCollectionData()

Evict all cache data from every cache region to which some collection role is assigned.

Since
5.3
evictDefaultQueryRegionback to summary
public void evictDefaultQueryRegion()

Evict all cached query results from the default region.

evictEntityDataback 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.

Parameters
entityClass:Class<?>

The entity type

identifier:Object

The entity identifier

Since
5.3
evictEntityDataback 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.

Parameters
entityName:String

The entity name

identifier:Object

The entity identifier

Since
5.3
evictEntityDataback 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.

Parameters
entityClass:Class<?>

The entity type

Since
5.3
evictEntityDataback 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.

Parameters
entityName:String

The entity name

Since
5.3
evictEntityDataback to summary
public void evictEntityData()

Evict all cached data from every cache region to which any entity type is assigned.

Since
5.3
evictNaturalIdDataback to summary
public void evictNaturalIdData(Class<?> entityClass)

Evict all cached natural id mappings for the given entity type.

Parameters
entityClass:Class<?>

The entity type

Since
5.3
evictNaturalIdDataback to summary
public void evictNaturalIdData(String entityName)

Evict all cached natural id mappings for the entity type with the given name.

Parameters
entityName:String

The entity name

Since
5.3
evictNaturalIdDataback to summary
public void evictNaturalIdData()

Evict all cached natural id mappings for every entity type.

Since
5.3
evictQueryRegionback to summary
public void evictQueryRegion(String regionName)

Evict all cached query results from the region with the given name.

Parameters
regionName:String

The cache name associated to the queries being cached.

evictQueryRegionsback to summary
public void evictQueryRegions()

Evict all cached query results from every region.

evictRegionback to summary
public void evictRegion(String regionName)

Evict all cached data from the named cache region.

Since
5.3
getSessionFactoryback to summary
public SessionFactory getSessionFactory()

The SessionFactory to which this Cache belongs.

Returns:SessionFactory

The SessionFactory