Package org.hibernate.annotations

Annotation Type Cache


  • @Target({TYPE,METHOD,FIELD})@Retention(RUNTIME)public @interfaceCache
    Marks a root entity or collection for second-level caching, and specifies:

    This annotation should always be used in preference to the less useful JPA-defined annotationCacheable, since JPA provides no means to specify anything about the semantics of the cache. Alternatively, it's legal, but redundant, for the two annotations to be used together.

    Note that entity subclasses of a root entity with a second-level cache inherit the cache belonging to the root entity.

    For example, the following entity is eligible for caching:

     @Entity @Cache(usage = NONSTRICT_READ_WRITE) public static class Person { ... }

    Similarly, this collection is cached:

     @OneToMany(mappedBy = "person") @Cache(usage = NONSTRICT_READ_WRITE) private List<Phone> phones = new ArrayList<>();

    Note that the second-level cache is disabled unless"hibernate.cache.region.factory_class" is explicitly specified, and so, by default, this annotation has no effect.

    See Also:
    Cacheable,Cache,CacheSettings.CACHE_REGION_FACTORY,CacheSettings.USE_SECOND_LEVEL_CACHE
    • Optional Element Summary

      Optional Elements 
      Modifier and TypeOptional ElementDescription
      Stringinclude
      Deprecated.
      UseincludeLazy() for the sake of typesafety.
      booleanincludeLazy
      When bytecode enhancement is used, andfield-level lazy fetching is enabled, specifies whether lazy attributes of the entity are eligible for inclusion in the second-level cache, in the case where they happen to be loaded.
      Stringregion
      The cache region name.
      • region

        String region
        The cache region name.
        Default:
        ""
      • includeLazy

        boolean includeLazy
        When bytecode enhancement is used, andfield-level lazy fetching is enabled, specifies whether lazy attributes of the entity are eligible for inclusion in the second-level cache, in the case where they happen to be loaded.

        By default, a loaded lazy fieldwill be cached when second-level caching is enabled. If this is not desirable—if, for example, the field value is extremely large and only rarely accessed—then setting@Cache(includeLazy=false) will prevent it and other lazy fields of the annotated entity from being cached, and the lazy fields will always be retrieved directly from the database.

        See Also:
        LazyGroup
        Default:
        true
      • include

        @DeprecatedString include
        Deprecated.
        UseincludeLazy() for the sake of typesafety.
        When bytecode enhancement is used, andfield-level lazy fetching is enabled, specifies which attributes of the entity are included in the second-level cache, either:
        • "all" properties, the default, or
        • only"non-lazy" properties.
        Default:
        "all"