Java 如何在 Hibernate 4.3 中配置二级缓存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20719590/
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-08-13 03:47:48  来源:igfitidea点击:

How to configure second level cache in Hibernate 4.3

javahibernatesecond-level-cache

提问by Prashant Shilimkar

I have read post related with this but not get any answer working for me. I am configuring second level cachein Hibernate v4.3.And I have used MySQL 5.0

我已阅读与此相关的帖子,但没有得到任何对我有用的答案。我配置second level cacheHibernate v4.3.,我已经使用MySQL 5.0

I have written following elements in hibernate.cfg.xml

我写了以下元素 hibernate.cfg.xml

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

I have annotated my Entity class for cache as follows

我已经为缓存注释了我的实体类,如下所示

@Entity 

@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Employee { ....}

Following exception is shown when run

运行时显示以下异常

INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:197)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:295)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2442)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2438)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1855)
    at com.example.hibernate.Hibernate4Main.main(Hibernate4Main.java:32)
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory]
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:101)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:46)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:83)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:223)
    ... 7 more
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.ehcache.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:87)
    ... 10 more
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:197)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:295)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2442)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2438)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1855)
    at com.example.hibernate.Hibernate4Main.main(Hibernate4Main.java:32)
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory]
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:101)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:46)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:83)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:223)
    ... 7 more
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.ehcache.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory]
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128)
    at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:87)
    ... 10 more

I have seen than there are different cache providers for Hibernate v3like EhCacheProvoider. All are in org.hibernate.cachepackage. But for Hibernate 4.3there are only 3 classes as RegionFactory.classand other two are of exception.

我已经看到有不同的缓存提供程序Hibernate v3like EhCacheProvoider。全部都在org.hibernate.cache包装中。但是因为Hibernate 4.3只有 3 个类,RegionFactory.class而其他两个类是exception.

1. What is wrong with above code?

1. 上面的代码有什么问题?

2. What are major changes made for Second level cache configuration in Hibernate 4.3?

2. Hibernate 4.3 的二级缓存配置有哪些主要变化?

回答by namiya

I got the same problem before. I added slf4j-api-1.6.1.jar to the project and fixed this problem. I was using Hibernate 4.3.5.

我之前也遇到过同样的问题。我在项目中添加了 slf4j-api-1.6.1.jar 并解决了这个问题。我使用的是 Hibernate 4.3.5。

回答by cmorris

I solved this for my configuration. Viewing the "effective pom" for my project had shown:

我为我的配置解决了这个问题。查看我的项目的“有效 pom”显示:

<dependencyManagement>
  <dependencies>        
    ...
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>4.3.7.Final</version>
    </dependency>
    ...
  </dependencies>
</dependencyManagement>

along with most of my other dependencies.

以及我的大多数其他依赖项。

Copying that hibernate-ehcache dependency into my actual project pom file added a second entry for it outside of the <dependencyManagement/>tag and that solved my problem. I had thought that because it was already included in the effective pom I didn't need to add it but apparently that is not the case for hibernate-ehcache as it seems to be for other packages.

将 hibernate-ehcache 依赖项复制到我的实际项目 pom 文件中,在<dependencyManagement/>标签之外为其添加了第二个条目,这解决了我的问题。我原以为因为它已经包含在有效的 pom 中,所以我不需要添加它,但显然 hibernate-ehcache 并非如此,因为它似乎适用于其他软件包。

回答by Surabhi Mutha

Refer this - http://architects.dzone.com/articles/hibernate-4-and-ehcache-higher

参考这个 - http://architects.dzone.com/articles/hibernate-4-and-ehcache-higher

add hibernate-ehcache jar to your project, that will solve the problem.

将 hibernate-ehcache jar 添加到您的项目中,这将解决问题。

回答by Swapnil

Your pom.xml file should look like below

您的 pom.xml 文件应如下所示

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.3.7.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.1</version>
        </dependency>

and Your hibernate.cfg.xml should contain the following configuration

并且您的 hibernate.cfg.xml 应包含以下配置

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>