16

I am getting the below exception when I am trying to test the spring and hibernate integration.

Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider
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)
... 18 more

application-context

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<!-- Datasource beans -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="url"  value="jdbc:mysql://localhost:3306/sterlingschema" />
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="username" value="root" />
  <property name="password" value="root" />
</bean>

<!--  Hibernate Template session factory bean -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
       <props>
           <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
           <prop key="show_sql">true</prop>
           <prop key="hibernate.hbm2ddl.auto">update</prop>
       </props>
    </property>

    <property name="mappingResources">
       <list>
           <value>/org/droidaceapps/domain/users.hbm.xml</value>
       </list>
     </property>

</bean> 

<!--  Hibernate template beans -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<!--  DAO beans -->

<bean id="userDAO"  class="org.droidaceapps.dao.UserDAOImpl">
  <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

<!--  Service beans -->

<bean id="userService"  class="org.droidaceapps.services.UserServiceImpl">
    <property name="userDAO" ref="userDAO" />
</bean> 

When I googled on this problem some said that we should be using springframework.orm.hibernate4 instead of springframework.orm.hibernate3. I did that. But still I am getting the same exception.

Can someone help me in Identifying what am I missing here.

Thanks

7 Answers 7

6

Note : org.hibernate.cache.Provider was removed in the change from Hibernate 3 to Hibernate 4.

If you use hibernate 3, you may get the java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider. If you change to hibernate 4, you will not get that, but you firstly should add hibernate4.jar and spring-orm.jar.

Sign up to request clarification or add additional context in comments.

Comments

2

It sounds liken you are missing hibernate dependency in your project set up:

Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider

Try downloading hibernate-core.jar and add to your project classpath.

If you use Maven, simply add the following dependency to your project's pom.xml:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>${hibernate.version}</version>
</dependency>

Hope that helps.

1 Comment

I have added this dependency using version 4.2.7, but there is no CacheProvider class in there.
0

You are injecting HibernateTransactionManager into DAO class instead of HibernateTemplate. Please check your configuration your file.

Comments

-1

This solution worked for me too:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.10.Final</version>
</dependency>

In my case, I already had the hibernate-entitymanager dependency, but was attempting to use the latest version of hibernate - 4.3.1.Final. Not sure why the downgrade was necessary here, but chances are the "Simple Spring JPA Utility Project" I was basing off in STS 3.5.0.M2 of is also a little dated?

Comments

-1

You have to change in spring configuration file.
change :

HibernateTemplate class as "org.springframework.orm.hibernate3.HibernateTemplate"
,and change:

LocalSessionFactoryBean bean as "org.springframework.orm.hibernate3.LocalSessionFactoryBean"

it will work.

Comments

-1

This might be because you are using "org.springframework.orm.hibernate3.LocalSessionFactoryBean" instead of "org.springframework.orm.hibernate4.LocalSessionFactoryBean" for "sessionFactory" in spring configuration file.

3 Comments

OP is using org.springframework.orm.hibernate4.LocalSessionFactoryBean
In my case, LocalSessionFactoryBean of hibernate3 picked up further I changed the dependency to use 4 and it got resolved.
Well, your case is not what this question covers. You can ask your own question and answer it, or you can check if another question for your case already exists. OP already states in the question that they switched from 3 to 4. Your answer doesn't answer OPs question.
-2

If you're using Maven, try putting this in your POM file:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.10.Final</version>
</dependency>

That worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.