lang_niu的专栏

有兴趣的可以加我微信:abc303030123

Second-level cache is not enabled for usage 异常

今天在使用Hibernate时,出现了Could not instantiate cache implementation的错误,我是在使用Hibernate tools时报的错误,应该运行时也会出现这样的错误,异常堆栈:

org.hibernate.HibernateException: Could not instantiate cache implementation
at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:64)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:214)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at ornsole.ConsoleConfiguration$3.execute(ConsoleConfiguration.java:357)
at ornsole.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:65)
at ornsole.ConsoleConfiguration.execute(ConsoleConfiguration.java:88)
at ornsole.ConsoleConfiguration.buildSessionFactory(ConsoleConfiguration.java:352)
at org.hibernatnsole.workbench.LazySessionFactoryAdapter.getChildren(LazySessionFactoryAdapter.java:41)
at org.hibernatnsole.workbench.BasicWorkbenchAdapter.getChildren(BasicWorkbenchAdapter.java:97)
at org.hibernatnsole.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:103)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:196)
at orre.internal.jobs.Worker.run(Worker.java:55)
Caused by: org.hibernate.cache.NoCachingEnabledException: Second-level cache is not enabled for usage [hibernate.cache.use_second_level_cache | hibernate.cache.use_query_cache]
at org.hibernate.cache.NoCacheProvider.buildCache(NoCacheProvider.java:21)
at org.hibernate.cache.CacheFactory.createCache(CacheFactory.java:61)
… 11 more

大概意思是二级缓存不可用,但是项目在开发阶段不需要启动缓存,所以就没有配置。
最后发现问题就是在这里,因为在Hibernate的bean的hbm配置文件中配置了缓存,而在hibernate的配置中没有配置提供的缓存机制,在 早起的hibernate的早起版本中默认是提供ehcache的,但是在最近的版本中已经不提供默认配置了。所以必须自己手工配置。在 hibernate的配置中加入如下片段即可

<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lang_niu/article/details/51560054
个人分类: java
上一篇“Bean named XXX must be of type[XXX], but was actually of type[XXX]”
下一篇localStorage也可以限时保存登录信息
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭