droidsites
droidsites

Reputation: 1035

java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider exception while integrating spring and hiberate

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

Upvotes: 16

Views: 39900

Answers (7)

Niraj Jha
Niraj Jha

Reputation: 615

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.

Upvotes: -1

user6352557
user6352557

Reputation:

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

Upvotes: 0

koti
koti

Reputation: 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.

Upvotes: -1

Dave McLure
Dave McLure

Reputation: 27

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?

Upvotes: -1

Jon
Jon

Reputation: 1

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.

Upvotes: -2

cameron
cameron

Reputation: 3006

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.

Upvotes: 6

yorkw
yorkw

Reputation: 41126

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.

Upvotes: 2

Related Questions