在配置hibernate4的二级缓存时,启动报错
org.hibernate.cache.NoCacheRegionFactoryAvailableException
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.spi.CacheImplementor]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:186)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:150)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:264)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1798)
at com.bjsxt.hibernate.HibernateCacheTest.beforeClass(HibernateCacheTest.java:22)
at com.bjsxt.hibernate.HibernateCacheTest.main(HibernateCacheTest.java:55)
Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given, please either disable second level cache or set correct region factory class name to property hibernate.cache.region.factory_class (and make sure the second level cache provider, hibernate-infinispan, for example, is available in the classpath).
at org.hibernate.cache.internal.NoCachingRegionFactory.buildTimestampsRegion(NoCachingRegionFactory.java:87)
at org.hibernate.cache.spi.UpdateTimestampsCache.<init>(UpdateTimestampsCache.java:62)
at org.hibernate.internal.CacheImpl.<init>(CacheImpl.java:72)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:40)
at org.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:35)
at org.hibernate.service.internal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:91)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:176)
... 7 more
下面是我在hibernate.cfg.xml中的配置
<!-- 开启二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true
</property>
<!-- 设置二级缓存插件EHCache的Provider类-->
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>
<property name="cache.use_query_cache">
true
</property>
已经导入了hibernate文件夹中ehcache需要的所有jar
后来百度后发现,在hibernate.cfg.xml配置有错误,在hibernate4中,设置二级缓存的配置已经不再是上面的配置,上面用到的还是hibernate3的,正确配置应该是
<!-- 开启二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 设置二级缓存插件EHCache的Provider类-->
<property name="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.EhCacheRegionFactory
</property>