HHH90001006:缺少缓存 [default-update-timestamps-region] 是即时创建的

问题描述

带有 EhCache 3 和 Hibernate 5 且启用查询和二级缓存的 Spring Data JPA 的简单 Spring Boot 2.5 application

spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true

在启动时为每个缓存生成来自 Hibernate 的警告和来自 EhCache 的信息消息:

WARN org.hibernate.orm.cache          HHH90001006: Missing cache[default-update-timestamps-region] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.
INFO org.ehcache.core.EhcacheManager  Cache 'default-update-timestamps-region' created in EhcacheManager.

WARN org.hibernate.orm.cache          HHH90001006: Missing cache[default-query-results-region] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.
INFO org.ehcache.core.EhcacheManager  Cache 'default-query-results-region' created in EhcacheManager.

这些 Hibernate 缓存的正确 EhCache 配置是什么?

解决方法

这里有一个很好的解释:https://www.ehcache.org/documentation/2.8/integrations/hibernate.html#configure-

如果您希望这些警告消失,您可以在 ehcache.xml 中明确定义缓存,或者使用 create 配置值作为警告日志。

,

Disable expiration on the default-update-timestamps-region cache as recommended by Hibernate

@Configuration @EnableCaching
public class ApplicationConfiguration {
    @Bean
    public CacheManager ehCacheManager() {
        CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();

        hibernateDefaultUpdateTimestampsRegionConfiguration(cacheManager);
        hibernateDefaultQueryResultsRegion(cacheManager);

        return cacheManager;
    }

    /**
     * Create Hibernate's default-update-timestamps-region cache. No expiration per Hibernate recommendation:
     * https://github.com/hibernate/hibernate-orm/blob/main/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc#query-cache-regions
     *
     * @param cacheManager
     */
    private void hibernateDefaultUpdateTimestampsRegionConfiguration(CacheManager cacheManager) {
        CacheConfigurationBuilder<Object,Object> builder = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(Object.class,Object.class,ResourcePoolsBuilder.newResourcePoolsBuilder().offheap(1,MemoryUnit.MB));

        javax.cache.configuration.Configuration<Object,Object> cache = Eh107Configuration.fromEhcacheCacheConfiguration(builder);

        cacheManager.createCache("default-update-timestamps-region",cache);

        //cacheManager.createCache("default-update-timestamps-region",new MutableConfiguration<>());
    }

    /**
     * Create Hibernate's default-query-results-region cache.
     * https://github.com/hibernate/hibernate-orm/blob/main/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc#query-cache-regions
     *
     * @param cacheManager
     */
    private void hibernateDefaultQueryResultsRegion(CacheManager cacheManager) {
        CacheConfigurationBuilder<Object,Object> cache = Eh107Configuration.fromEhcacheCacheConfiguration(builder);

        cacheManager.createCache("default-query-results-region",cache);

        //cacheManager.createCache("default-query-results-region",new MutableConfiguration<>());
    }

}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...