java 使用 Hibernate 时如何删除 Ehcache WARN 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29947671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:10:03  来源:igfitidea点击:

How to remove the Ehcache WARN messages when using Hibernate

javahibernateloggingconfigurationehcache

提问by degr

I have this Ehcache XML configuration:

我有这个 Ehcache XML 配置:

<ehcache>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskSpoolBufferSizeMB="30"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
</ehcache>

And also, I have some packages with entities (around 150). If I deploy my application on tomcat server, there is a lot WARN messages in log:

而且,我有一些带有实体的包(大约 150 个)。如果我在 tomcat 服务器上部署我的应用程序,日志中有很多警告消息:

2015-04-29 11:59:02,712 [RMI TCP Connection(3)-127.0.0.1] WARN org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [com.company.project.entity.package.MyEntity]; using defaults.

2015-04-29 11:59:02,712 [RMI TCP Connection(3)-127.0.0.1] WARN org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003:找不到名为 [com.company. project.entity.package.MyEntity]; 使用默认值。

I can write configuration for each entity -

我可以为每个实体编写配置 -

<cache name="com.company.project.entity.package.MyEntity"
                      maxEntriesLocalHeap="50"
                      eternal="false"
                      overflowToDisk="false"
                    timeToLiveSeconds="120">
                <persistence strategy="localTempSwap"/>
        </cache>

But in this way, my configuration file become too large (1600 rows). I think there is exist another way to set default config to each entity and kill warn messages, but I don't know how to do it. If anybody know, please help. Many thanks.

但是这样一来,我的配置文件就变得太大了(1600 行)。我认为还有另一种方法可以为每个实体设置默认配置并终止警告消息,但我不知道该怎么做。如果有人知道,请帮忙。非常感谢。

采纳答案by Vlad Mihalcea

This is the Hibernate EHCache Cache Region code that logs the warning message:

这是记录警告消息的 Hibernate EHCache 缓存区域代码:

private Ehcache getCache(String name) throws CacheException {
    try {
        Ehcache cache = manager.getEhcache( name );
        if ( cache == null ) {
            LOG.unableToFindEhCacheConfiguration( name );
            manager.addCache( name );
            cache = manager.getEhcache( name );
            LOG.debug( "started EHCache region: " + name );
        }
        HibernateEhcacheUtils.validateEhcache( cache );
        return cache;
    }
    catch (net.sf.ehcache.CacheException e) {
        throw new CacheException( e );
    }

}

As you can see, you have two options:

如您所见,您有两个选择:

  1. You either declare the cache region in the ehcache.xmlfile.
  2. You set the 'org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory' log level to ERROR:

    For Log4j2:

    <Logger name="org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory" level="error">
        <AppenderRef ref="File"/>
    </Logger>
    
  1. 您可以在ehcache.xml文件中声明缓存区域。
  2. 您将“org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory”日志级别设置为错误:

    对于 Log4j2:

    <Logger name="org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory" level="error">
        <AppenderRef ref="File"/>
    </Logger>
    

回答by Andrew McKee

One option would be to create a CacheObject base entity and configure that as your cache object.

一种选择是创建一个 CacheObject 基本实体并将其配置为您的缓存对象。

Not the cleanest of solution but would remove the warnings without adding a large amount of config.

不是最干净的解决方案,但会在不添加大量配置的情况下删除警告。

As the warning is just that, a warning personally i would just configure it to not show.

由于警告就是这样,个人警告我只会将其配置为不显示。