Exception NoClassDefFoundError for CacheProvider

I'm kind of new in Spring and hibernate so I'm trying to implement some simple web application based on Spring 3 + hibernate 4 while I start tomcat I have this exception:

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)

I've found that this class was in hibernate-core for hibernate 3 but I've not found it in hibernate 4.

The part of my context.xml for persistence:

<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>

Please help me to figure out why it's trying to load CacheProvider because I dont have any settings for that in context.xml and which jar I have to add in my project. Thanks!


Solution 1:

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.

Solution 2:

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.