java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider 在集成 spring 和 hiberate 时出现异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11978094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:18:55  来源:igfitidea点击:

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

springhibernate

提问by droidsites

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

当我尝试测试 spring 和 hibernate 集成时,出现以下异常。

Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.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.

当我搜索这个问题时,有人说我们应该使用 springframework.orm.hibernate4 而不是 springframework.orm.hibernate3。我就是这么做的。但我仍然遇到同样的例外。

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

有人可以帮助我确定我在这里遗漏了什么。

Thanks

谢谢

回答by cameron

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

注意:org.hibernate.cache.Provider 在从 Hibernate 3 到 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.jarand spring-orm.jar.

如果您使用 hibernate 3,您可能会获得java.lang.ClassNotFoundException: org.hibernate.cache.CacheProvider. 如果您更改为 hibernate 4,您将不会得到那个,但您首先应该添加hibernate4.jarspring-orm.jar

回答by yorkw

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

听起来您在项目设置中缺少 hibernate 依赖项:

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

引起:java.lang.ClassNotFoundException:org.hibernate.cache.CacheProvider

Try downloading hibernate-core.jarand add to your project classpath.

尝试下载hibernate-core.jar并添加到您的项目类路径中。

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

如果您使用 Maven,只需将以下依赖项添加到您的项目的 pom.xml 中:

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

Hope that helps.

希望有帮助。

回答by yorkw

You are injecting HibernateTransactionManagerinto DAOclass instead of HibernateTemplate. Please check your configuration your file.

您正在将HibernateTransactionManager注入DAO类而不是HibernateTemplate。请检查您的配置文件。

回答by Niraj Jha

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.

这可能是因为您在 spring 配置文件中为“sessionFactory”使用“org.springframework.orm.hibernate3.LocalSessionFactoryBean”而不是“org.springframework.orm.hibernate4.LocalSessionFactoryBean”。

回答by Dave McLure

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?

就我而言,我已经有了 hibernate-entitymanager 依赖项,但我正在尝试使用最新版本的 hibernate - 4.3.1.Final。不知道为什么这里有必要降级,但我在 STS 3.5.0.M2 中基于的“Simple Spring JPA Utility Project”有可能也有点过时了?

回答by koti

You have to change in spring configuration file.
change :

您必须在 spring 配置文件中进行更改。
改变 :

HibernateTemplate classas "org.springframework.orm.hibernate3.HibernateTemplate"
,and change:

HibernateTemplate class作为"org.springframework.orm.hibernate3.HibernateTemplate"
,并改变:

LocalSessionFactoryBean beanas "org.springframework.orm.hibernate3.LocalSessionFactoryBean"

LocalSessionFactoryBean bean作为 "org.springframework.orm.hibernate3.LocalSessionFactoryBean"

it will work.

它会起作用。

回答by Jon

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

如果您使用的是 Maven,请尝试将其放入您的 POM 文件中:

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

That worked for me.

那对我有用。