6

我在这里阅读了有关此主题的其他主题,但没有一个解决方案适合我。

我试着把它放在我的 hibernate.cfg.xml 中:

<property name="hibernate.cache.region.factory_class">org.hibernate.cache.spi.EntityRegion</property>

我总是收到此错误: 无法实例化 RegionFactory [org.hibernate.cache.spi.EntityRegion]

我还尝试了来自 Hibernate 网站上线程的大多数建议,但没有运气。

那么我该如何配置呢?

4

3 回答 3

12

好吧,我找到了答案(来自 Youtube 用户):

  1. 使用 hibernate-release-4.1.0.Final 或更高版本。
  2. 从 lib\optional\ehcache 目录添加 jars
  3. 更新hibernate.cfg.xml

    <property name="cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</property>
    
  4. 添加 slf4j-api-1.6.1.jar(我在从 ehcache.org 下载的 ehcache-2.5.1-distribution.tar.gz 中另外找到)因为 ClassNotFoundException。

  5. 将此添加到您的 hibernate.cfg.xml 中:

    <!-- Enable Hibernate's automatic session context management -->
    <property name="cache.use_second_level_cache">true</property>
    

这里的关键点是从 Hibernate4 的 optional\ 目录中添加 ehcache jar。

于 2012-08-06T16:56:22.007 回答
3

将以下属性添加到 Hibernate 属性:

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">hibernate-ehcache.xml</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop> 

您需要 hibernate-ehcache 4.1.1 jar 或不需要 ehcache jar。

请记下已更改的缓存提供程序类。

于 2012-08-23T19:04:34.973 回答
2

希望,它可能对某些人有用(hibernate 4.x)。

我的弹簧配置摘录:

public Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
    properties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
    properties.setProperty("hibernate.format_sql", "true");
    properties.setProperty("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));
    // second-level cache:
    properties.setProperty("hibernate.cache.use_second_level_cache", "true");        
    properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.EhCacheRegionFactory");
    properties.setProperty("net.sf.ehcache.configurationResourceName", env.getProperty("net.sf.ehcache.configurationResourceName"));
    return properties;
}

您的类路径中必须有 'org.hibernate:hibernate-ehcache:HIBERNATE_VERSION'。

请参阅:关于休眠配置的 Ehcache 文档

于 2014-06-02T11:40:56.797 回答