下面是我的项目层次结构的插图
project hierarchy http://a.yfrog.com/img809/3151/58400945.png
当我尝试将我的file_name.java文件连接到hibernate时,我遇到了这些错误
Exception in thread "main" 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:1218)
at net.pkg.dao.FirstExample.main(FirstExample.java:17)
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)
问题与我的hibernate.cfg.xml
有关......但我无法解决。有什么想法吗?
答案 0 :(得分:1)
我怀疑您在实体上使用@Cacheable
或@Cache
而没有激活第二级缓存,因此出现错误消息:
o.h.c.NoCachingEnabledException: Second-level cache is not enabled for usage ...
你需要在hibernate.cfg.xml
中使用二级缓存(我在这里使用EHCache作为缓存提供者):
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
你是否真的需要二级缓存是另一回事。