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

时间:2013-12-21 13:38:08

标签: java hibernate second-level-cache

我已阅读与此相关的帖子,但没有得到任何答案为我工作。 我正在second level cache中配置Hibernate v4.3.而我使用了MySQL 5.0

我在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>

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

@Entity 

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

运行时会显示以下异常

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

我见过Hibernate v3 EhCacheProvoider之类的缓存提供商不同。org.hibernate.cache。全部都在Hibernate 4.3包中。 但对于RegionFactory.class,只有3个类为exception,其他两个为{{1}}。

1。上面的代码出了什么问题?

2。在Hibernate 4.3中对二级缓存配置做了哪些重大更改?

4 个答案:

答案 0 :(得分:6)

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

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

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

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

答案 1 :(得分:3)

您的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>

并且您的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>

答案 2 :(得分:1)

请参阅 - http://architects.dzone.com/articles/hibernate-4-and-ehcache-higher

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

答案 3 :(得分:0)

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