本文介绍了何时/如何以编程方式设置Hibernate大小使用的Ehcache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过添加以下内容在Hibernate 4.3.11中启用了二级缓存:

I have enabled 2nd level caching in Hibernate 4.3.11 by adding:

 config.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
 config.setProperty("hibernate.cache.use_second_level_cache", "true");

进入我的Hibernate Config.

to my Hibernate Config.

这到我的pom.xml中(不确定是否有必要使pom定义如此尴尬)

This to my pom.xml (Not sure if necessary for pom definition to be this awkward)

<dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>4.3.11.Final</version>
      <exclusions>
          <exclusion>
              <groupId>net.sf.ehcache</groupId>
              <artifactId>ehcache-core</artifactId>
          </exclusion>
      </exclusions>
      </dependency>
      <dependency>
          <groupId>net.sf.ehcache</groupId>
          <artifactId>ehcache</artifactId>
          <version>2.7.0</version>
      </dependency>

这是我要缓存的类

@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

但是创建数据库时如何在代码中配置缓存大小,使用Xml文件对我来说不现实,因为Xml文件只会使我更喜欢在代码中进行构建的过程复杂化.

But how do I configure the cache size in code when the database is created, its not practical for me to use Xml file that just adds complication to the build process I would much prefer to do in code.

更新,从Hibernate创建数据库后,我发现缓存已经创建

Update, after creating database from Hibernate, I find the Caches are already created

CacheManager.create();
String[] cacheNames = CacheManager.getInstance().getCacheNames();
for(String cacheName:cacheNames)
{
    MainWindow.logger.severe("CacheName:"+cacheName);
    Cache cache = CacheManager.getInstance().getCache(cacheName);
    cache.getCacheConfiguration().setMaxEntriesInCache(1000);
    cache.getCacheConfiguration().setLogging(true);
}

但是我如何影响它们的创建方式或像我所做的那样修改值足以更新.运行时,我看不到调试信息,也没有任何迹象表明正在使用缓存.

but how can I affect how they are created or does modifying values like i have done is enough to update. When I run I see no debugging outout or anything to indicate the cache is being used.

推荐答案

您可以子类化org.hibernate.cache.ehcache.EhCacheRegionFactory并手动执行任何缓存配置,然后告诉Hibernate通过以下方式使用您的自定义缓存工厂:

You could subclass org.hibernate.cache.ehcache.EhCacheRegionFactory and perform any cache configuration manually, and then tell Hibernate to use your custom cache factory with:

Configuration.setProperty("hibernate.cache.region.factory_class", "my.cache.FactoryClass");

请参阅: http://www.ehcache.org/documentation/2.7/integrations/hibernate.html#set-the-hibernate-cache-provider-programmatically-

这篇关于何时/如何以编程方式设置Hibernate大小使用的Ehcache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:58