How to set up second level cache in Hibernate 4.3

I read the post related to this, but do not get a response to me. I am setting up a second level cache in Hibernate v4.3. And I used MySQL 5.0

I wrote the following elements in 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 annotated my cache Entity class as follows

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

The following exception is displayed at startup

 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 saw how there are different cache providers for Hibernate v3 , for example EhCacheProvoider . All are in the org.hibernate.cache package. But for Hibernate 4.3 there are only 3 classes like RegionFactory.class , and the other two are from exception .

1. What is wrong with the above code?

2. What are the main changes made to configure the second level cache in Hibernate 4.3?

+8
java hibernate second-level-cache
source share
4 answers

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

 <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 addictions.

Copying this hibernate-ehcache dependency into my actual project pom file added a second entry for it outside the <dependencyManagement/> , and this solved my problem. I thought that since it was already included in the efficient pom, I did not need to add it, but apparently this is not the case for hibernate-ehcache, since it is like other packages.

+6
source share

Your pom.xml file should look below

  <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

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

Please note: http://architects.dzone.com/articles/hibernate-4-and-ehcache-higher

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

+1
source share

I have the same problem. I added the slf4j-api-1.6.1.jar project and fixed this problem. I used Hibernate 4.3.5.

0
source share

All Articles