CacheProvider 的异常 NoClassDefFoundError

我对 Spring 和 hibernate 还是个新手,所以我试着在 Spring 3 + hibernate 4的基础上实现一些简单的 web 应用程序 当我开始做公猫的时候,我有一个例外:

java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethods(Class.java:1791)
...
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)

我发现这个类在 hibernate 3的 hibernate-core 中,但是在 hibernate 4中没有找到。

Xml 中用于持久化的部分:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:oracle:thin:@IP_Address:SID"/>
<property name="username" value="xxx"/>
<property name="password" value="xxx"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="20"/>
</bean>


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.huawei.vms.user"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>

请帮助我弄清楚它为什么试图加载 CacheProvider,因为我在 context.xml 中没有这方面的任何设置,我必须在我的项目中添加哪个 jar。 谢谢!

78310 次浏览

This might be due to changes introduced in Hibernate 4 that are not compatible with Spring support for Hibernate. This issue talks about adding separate package to support hibernate 4. You will need spring 3.1 for this. The other option is to stick to hibernate 3 if you don't need any specific feature introduced in 4.

Change your AnnotationSessionFactoryBean to org.springframework.orm.hibernate4.LocalSessionFactoryBean (Hibernate 4) and you'll be good to go. The AnnotationSessionFactoryBean was replaced with the LocalSessionFactoryBean as it does class path scanning now.

updating AnnotationSessionFactoryBean to hibernate4 works perfect. Also make sure your transactionManager also points to hibernate4,

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="PACKAGE_NAME"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>


<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

A really simple problem that will cause the same error is simply to have a mismatch between the hibernate version in the pom (4.something) and the version specified in the spring config.