hibernate中异常:java.lang.ClassNotFoundException: org.hibernate.cache.internal.NoCacheProvider

来源:互联网 发布:学习数据库心得体会 编辑:程序博客网 时间:2024/05/11 00:17
java.lang.ExceptionInInitializerError
    at com.bjsxt.registration.service.UserManager.userExists(UserManager.java:15)
    at com.bjsxt.registration.service.UserManagerTest.testUserExists(UserManagerTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]
    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 com.bjsxt.registration.util.HibernateUtil.<clinit>(HibernateUtil.java:9)
    ... 25 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:409)
    ... 30 more
Caused by: org.hibernate.cache.CacheException: could not instantiate CacheProvider [org.hibernate.cache.internal.NoCacheProvider]
    at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.<init>(RegionFactoryCacheProviderBridge.java:66)
    ... 35 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.internal.NoCacheProvider
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
    at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.<init>(RegionFactoryCacheProviderBridge.java:63)

    ... 35 more

认真观察异常的信息

Caused by: org.hibernate.cache.CacheException: could not instantiate CacheProvider [org.hibernate.cache.internal.NoCacheProvider]

可知,异常原因为cache(缓存)的CacheException,所以,在jar包导入正常的情况下,我们就要检查配置文件了:

异常出现之前,我的配置文件hibernate.cfg.xml是这样写的----

<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- Database connection settings -->        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="connection.url">jdbc:mysql://localhost:3306/spring</property>        <property name="connection.username">root</property>        <property name="connection.password">1111</property>        <!-- JDBC connection pool (use the built-in) -->        <property name="connection.pool_size">1</property>        <!-- SQL dialect -->        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>        <!-- Enable Hibernate's automatic session context management -->        <property name="current_session_context_class">thread</property>        <!-- Disable the second-level cache  -->        <span style="color:#FF0000;"><property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property></span>        <!-- Echo all executed SQL to stdout -->        <property name="show_sql">true</property>        <property name="format_sql">true</property>        <!-- Drop and re-create the database schema on startup -->        <property name="hbm2ddl.auto">update</property>                <!-- <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>  通过xml映射        <mapping class="com.bjsxt.hibernate.Teacher"/>  通过Annotation映射 -->        <!-- <mapping class="com.bjsxt.hibernate.Husband"/>  通过Annotation映射 -->        <!-- <mapping class="com.bjsxt.hibernate.Wife"/>  通过Annotation映射 -->        <!-- <mapping class="com.bjsxt.hibernate.Group"/>  通过Annotation映射        <mapping class="com.bjsxt.hibernate.User"/>  通过Annotation映射 -->        <mapping class="com.bjsxt.registration.model.User"/>            </session-factory></hibernate-configuration>


如上的配置文件把二级缓存关闭了,导致CacheException,现在,需要打开二级缓存,也就是把关闭二级缓存的代码(配置文件中的红色字体)删掉或者注解掉,这样就可以了。

0 0
原创粉丝点击