[英]How to configure second level cache in Hibernate 4.3
I have read post related with this but not get any answer working for me. 我已阅读与此相关的帖子,但没有得到任何答案为我工作。 I am configuring
second level cache
in Hibernate v4.3.
我在
Hibernate v4.3.
配置second level cache
Hibernate v4.3.
And I have used MySQL 5.0
我使用的是
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类,如下所示
@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
I have seen than there are different cache providers for Hibernate v3
like EhCacheProvoider
. 我已经看到,
Hibernate v3
有不同的缓存提供程序,如EhCacheProvoider
。 All are in org.hibernate.cache
package. 所有都在
org.hibernate.cache
包中。 But for Hibernate 4.3
there are only 3 classes as RegionFactory.class
and other two are of exception
. 但是对于
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中二级缓存配置的主要变化是什么?
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. 将hibernate-ehcache依赖项复制到我的实际项目pom文件中,在
<dependencyManagement/>
标记之外为它添加了第二个条目,这解决了我的问题。 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. 我曾经想过,因为它已经包含在有效的pom中,我不需要添加它,但显然不是hibernate-ehcache的情况,因为它似乎适用于其他包。
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>
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添加到您的项目中,这将解决问题。
I got the same problem before. 我以前遇到过同样的问题。 I added slf4j-api-1.6.1.jar to the project and fixed this problem.
我在项目中添加了slf4j-api-1.6.1.jar并解决了这个问题。 I was using Hibernate 4.3.5.
我正在使用Hibernate 4.3.5。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.