【问题标题】:How to configure second level cache in Hibernate 4.1.5 SP1?如何在 Hibernate 4.1.5 SP1 中配置二级缓存?
【发布时间】:2012-08-03 15:58:10
【问题描述】:

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

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

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

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

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

那么我该如何配置呢?

【问题讨论】:

    标签: hibernate second-level-cache hibernate-4.x


    【解决方案1】:

    嗯,我找到了答案(来自 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-2.5.1-distribution.tar.gz 中找到它 另外从 ehcache.org 下载)因为 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。

    【讨论】:

      【解决方案2】:

      将以下属性添加到 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。

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

      【讨论】:

        【解决方案3】:

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

        摘自我的spring配置:

        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 doc on hibernate configuration

        【讨论】:

        • 链接坏了=(
        猜你喜欢
        • 1970-01-01
        • 2014-09-11
        • 2014-10-18
        • 2015-09-07
        • 1970-01-01
        • 2015-05-14
        • 2015-02-06
        • 1970-01-01
        • 2020-05-19
        相关资源
        最近更新 更多