93

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!

Bax
  • 4,260
  • 5
  • 43
  • 65
Fedor Skrynnikov
  • 5,521
  • 4
  • 28
  • 32
  • can you post the hibernate config file? – Cornel Creanga Sep 23 '11 at 12:42
  • I don't have it. Maybe it is misunderstanding and I have to add that I did exactly what was described in book "Spring in action 3" chapter "5.4 Integrating Hibernate with Spring". While I was doing that I had some problems with absent classes that I found in hibernate, so I added hibernate 4 into my project. But it wasn't enouph – Fedor Skrynnikov Sep 23 '11 at 12:55
  • 1
    are you sure hibernate 4 has that class? I'd try hibernate 3.6.x – Bozho Sep 23 '11 at 13:10
  • No. I've not found it in version 4. Actually, I have found it but in another package in ehcache. But the question is why tomcat trys to load it? And is it actually needed? – Fedor Skrynnikov Sep 23 '11 at 13:19
  • It's deprecated from what I know – Cornel Creanga Sep 23 '11 at 13:42

4 Answers4

184

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.

Marty Pitt
  • 28,822
  • 36
  • 122
  • 195
Aaron Douglas
  • 2,494
  • 1
  • 17
  • 14
  • Any other possible ideas? This does not seem to do anything for me. Do the properties need to be changed? – zod May 16 '12 at 16:28
  • 3
    Make sure you've changed the package from hibernate3 to hibernate4 - a LocalSessionFactoryBean exists in both packages. What version of Spring and Hibernate are you using? – Aaron Douglas May 23 '12 at 12:30
11

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.

gkamal
  • 20,777
  • 4
  • 60
  • 57
  • Yes. I've downgrated from hiberante 4 to version 3.6.7 and everything became working. Thanks! – Fedor Skrynnikov Sep 26 '11 at 10:39
  • 1
    +1, got it working with both your and [Aaron's](http://stackoverflow.com/a/8273320/56285) advice. (Or more precisely, got past this and run into other Hibernate 4 problems. :P) By the way, Spring 3.1 is no longer in RC (first GA release in December 2011). – Jonik Dec 29 '11 at 12:04
  • thx @gkamal. Down-versioning to hibernate 3 worked for me. Solution: I used hibernate-entitymanager and hibernate-core, both version 3.6.10.Final see http://mvnrepository.com/search.html?query=hibernate-entitymanager – Adriano Sep 27 '12 at 12:36
8

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>
Vishal Jagtap
  • 551
  • 1
  • 6
  • 9
0

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.

John Lockwood
  • 3,787
  • 29
  • 27