examples of @Cache and documentation around cache enablement

This commit is contained in:
Gavin 2022-12-26 12:01:10 +01:00 committed by Gavin King
parent 110a1f6a56
commit d886c56228
3 changed files with 35 additions and 1 deletions

View File

@ -79,11 +79,21 @@ package org.hibernate;
* 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.
* <p>
* 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 {@link org.hibernate.cache.spi.RegionFactory} to integrate a
* backend storage mechanism. Therefore, the second-level cache is completely disabled
* by default, unless {@value org.hibernate.cfg.AvailableSettings#CACHE_REGION_FACTORY}
* is explicitly specified. For convenience, the second-level cache may also be enabled
* or disabled using {@value org.hibernate.cfg.AvailableSettings#USE_SECOND_LEVEL_CACHE}.
*
* @author Steve Ebersole
*
* @see org.hibernate.annotations.Cache
* @see org.hibernate.annotations.CacheConcurrencyStrategy
* @see org.hibernate.cfg.AvailableSettings#CACHE_REGION_FACTORY
* @see org.hibernate.cfg.AvailableSettings#USE_SECOND_LEVEL_CACHE
*/
public interface Cache extends jakarta.persistence.Cache {
/**

View File

@ -32,9 +32,28 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* <p>
* Note that entity subclasses of a root entity with a second-level
* cache inherit the cache belonging to the root entity.
* <p>
* For example, the following entity is eligible for caching:
* <pre>{@code
* @Entity
* @Cache(usage = NONSTRICT_READ_WRITE)
* public static class Person { ... }
* }</pre>
* Similarly, this collection is cached:
* <pre>{@code
* @OneToMany(mappedBy = "person")
* @Cache(usage = NONSTRICT_READ_WRITE)
* private List<Phone> phones = new ArrayList<>();
* }</pre>
* Note that the second-level cache is disabled unless
* {@value org.hibernate.cfg.AvailableSettings#CACHE_REGION_FACTORY}
* is explicitly specified, and so, by default, this annotation has
* no effect.
*
* @see jakarta.persistence.Cacheable
* @see org.hibernate.Cache
* @see org.hibernate.cfg.AvailableSettings#CACHE_REGION_FACTORY
* @see org.hibernate.cfg.AvailableSettings#USE_SECOND_LEVEL_CACHE
*
* @author Emmanuel Bernard
*/

View File

@ -14,6 +14,7 @@ import org.hibernate.Incubating;
import org.hibernate.Interceptor;
import org.hibernate.SessionFactoryObserver;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cache.internal.NoCachingRegionFactory;
import org.hibernate.cache.spi.TimestampsCacheFactory;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.engine.jdbc.batch.spi.BatchBuilder;
@ -1360,6 +1361,9 @@ public interface AvailableSettings {
* <li>a {@link Class} implementing {@link org.hibernate.cache.spi.RegionFactory}, or
* <li>he name of a class implementing {@link org.hibernate.cache.spi.RegionFactory}.
* </ul>
* Defaults to {@link NoCachingRegionFactory}, so that caching is disabled.
*
* @see #USE_SECOND_LEVEL_CACHE
*/
String CACHE_REGION_FACTORY = "hibernate.cache.region.factory_class";
@ -1375,7 +1379,7 @@ public interface AvailableSettings {
*
* @since 5.2
*
* @deprecated this is only honored for hibernate-infinispan
* @deprecated this is only honored for {@code hibernate-infinispan}
*/
@Deprecated
String CACHE_KEYS_FACTORY = "hibernate.cache.keys_factory";
@ -1387,6 +1391,7 @@ public interface AvailableSettings {
* is not the {@link org.hibernate.cache.internal.NoCachingRegionFactory}, then
* the second-level cache is enabled. Otherwise, the second-level cache is disabled.
*
* @see #CACHE_REGION_FACTORY
* @see org.hibernate.boot.SessionFactoryBuilder#applySecondLevelCacheSupport(boolean)
*/
String USE_SECOND_LEVEL_CACHE = "hibernate.cache.use_second_level_cache";