配置Hibernate二级缓存时,不能初始化RegionFactory的解决办法

时间:2022-11-23 14:52:02

配置Hibernate 二级缓存时,出现以下bug提示:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.EhCacheRegionFactory]
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:423)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:280)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
at query.test.main(test.java:19)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.EhCacheRegionFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:409)
... 5 more

在网上找了各种帖子都不好使,都是说要么这个jar包没导,要么那个jar包没导。最后一怒之下把hibernate所有的包+mysql-connection-java等全部导入,运行!居然成功了。

但是具体是哪个还是没有确定,因为这个问题实在太纠结了,如果有大神在,还望指教!