Package org.hibernate

Enum Class CacheMode

java.lang.Object
java.lang.Enum<CacheMode>
org.hibernate.CacheMode
All Implemented Interfaces:
FindOption, Serializable, Comparable<CacheMode>, java.lang.constant.Constable

public enum CacheMode extends Enum<CacheMode> implements FindOption
Controls how the session interacts with the second-level cache or query cache. An instance of CacheMode may be viewed as packaging a JPA-defined CacheStoreMode with a CacheRetrieveMode. For example, PUT represents the combination (BYPASS, USE).

However, this enumeration recognizes only five such combinations. In Hibernate, CacheStoreMode.REFRESH always implies CacheRetrieveMode.BYPASS, so there's no CacheMode representing the combination (REFRESH, USE).

See Also:
  • Enum Constant Details

    • NORMAL

      public static final CacheMode NORMAL
      The session may read items from the cache, and add items to the cache as it reads them from the database.
    • IGNORE

      public static final CacheMode IGNORE
      The session will never interact with the cache, except to invalidate cached items when updates occur.
    • GET

      public static final CacheMode GET
      The session may read items from the cache, but will not add items, except to invalidate items when updates occur.
    • PUT

      public static final CacheMode PUT
      The session will never read items from the cache, but will add items to the cache as it reads them from the database. In this mode, the value of the configuration setting "hibernate.cache.use_minimal_puts" determines whether an item is written to the cache when the cache already contains an entry with the same key. Minimal puts should be:
      • disabled for a cache where writes and reads carry a similar cost, as is usually the case for a local in-memory cache, and
      • enabled for a cache where writes are much more expensive than reads, which is usually the case for a distributed cache.

      It's not usually necessary to specify this setting explicitly because, by default, it's set to a sensible value by the second-level cache implementation.

      See Also:
    • REFRESH

      public static final CacheMode REFRESH
      As with to PUT, the session will never read items from the cache, but will add items to the cache as it reads them from the database. But in this mode, the effect of the configuration setting "hibernate.cache.use_minimal_puts" is bypassed, in order to force a refresh of a cached item, even when an entry with the same key already exists in the cache.
      See Also:
  • Method Details

    • values

      public static CacheMode[] values()
      Returns an array containing the constants of this enum class, in the order they are declared.
      Returns:
      an array containing the constants of this enum class, in the order they are declared
    • valueOf

      public static CacheMode valueOf(String name)
      Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum class has no constant with the specified name
      NullPointerException - if the argument is null
    • getJpaStoreMode

      public CacheStoreMode getJpaStoreMode()
      Returns:
      the JPA-defined CacheStoreMode implied by this cache mode
    • getJpaRetrieveMode

      public CacheRetrieveMode getJpaRetrieveMode()
      Returns:
      the JPA-defined CacheRetrieveMode implied by this cache mode
    • isGetEnabled

      public boolean isGetEnabled()
      Does this cache mode indicate that reads are allowed?
      Returns:
      true if cache reads are allowed; false otherwise.
    • isPutEnabled

      public boolean isPutEnabled()
      Does this cache mode indicate that writes are allowed?
      Returns:
      true if cache writes are allowed; false otherwise.
    • interpretExternalSetting

      public static CacheMode interpretExternalSetting(String setting)
      Interpret externalized form as an instance of this enumeration.
      Parameters:
      setting - The externalized form.
      Returns:
      The matching enum value.
      Throws:
      MappingException - Indicates the external form was not recognized as a valid enum value.
    • fromJpaModes

      public static CacheMode fromJpaModes(CacheRetrieveMode retrieveMode, CacheStoreMode storeMode)
      Interpret the given JPA modes as an instance of this enumeration.