【问题标题】:Eror while creating bean on hibernate spring project在hibernate spring项目中创建bean时出错
【发布时间】:2021-11-28 22:12:06
【问题描述】:

我是 spring 新手,在这个项目中休眠我正在使用 netbeans v.8.2,我正在使用库 spring 3.2.7 和 hibernate 4.3.x 我应该怎么做才能修复错误?

错误: 在类路径资源 [applicationContext.xml] 中创建名称为“dao”的 bean 时出错:设置 bean 属性“模板”时无法解析对 bean“模板”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [applicationContext.xml] 中定义的名称为“模板”的 bean 时出错:设置 bean 属性“sessionFactory”时无法解析对 bean“mysessionFactory”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建类路径资源 [applicationContext.xml] 中定义的名称为“mysessionFactory”的 bean 时出错:bean 初始化失败;嵌套异常是 java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

这是应用程序 context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  
xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:p="http://www.springframework.org/schema/p"  
xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName"  value="com.mysql.jdbc.Driver"></property>  
    <property name="url" value="jdbc:mysql://localhost:3306/kampus"></property>  
    <property name="username" value="root"></property>  
    <property name="password" value=""></property>  
</bean>  
  
<bean id="mysessionFactory"  
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
    <property name="dataSource" ref="dataSource"></property>  
      
    <property name="mappingResources">  
    <list>  
    <value>Mahasiswa.hbm.xml</value>  
    </list>  
    </property>  
      
    <property name="hibernateProperties">  
        <props>  
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
            <prop key="hibernate.hbm2ddl.auto">update</prop>  
            <prop key="hibernate.show_sql">true</prop>  
              
        </props>  
    </property>  
</bean>  
  
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">  
<property name="sessionFactory" ref="mysessionFactory"></property>  
</bean>  
  
<bean id="dao" class="dao.MahasiswaDao">  
<property name="template" ref="template"></property>  
</bean>  
  
  
</beans>  

这是 mahasiswa.java(英语为 mahasiswa=student):

package entity;

public class Mahasiswa  implements java.io.Serializable {


 private String npm;
 private String nama;
 private String jurusan;
 private String alamat;

public Mahasiswa() {
}

public Mahasiswa(String npm, String nama, String jurusan, String alamat) {
   this.npm = npm;
   this.nama = nama;
   this.jurusan = jurusan;
   this.alamat = alamat;
}

public String getNpm() {
    return this.npm;
}

public void setNpm(String npm) {
    this.npm = npm;
}
public String getNama() {
    return this.nama;
}

public void setNama(String nama) {
    this.nama = nama;
}
public String getJurusan() {
    return this.jurusan;
}

public void setJurusan(String jurusan) {
    this.jurusan = jurusan;
}
public String getAlamat() {
    return this.alamat;
}

public void setAlamat(String alamat) {
    this.alamat = alamat;
}


}

这是 Mahasiswa.dao:

package dao;
import entity.Mahasiswa;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class MahasiswaDao {
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {  
this.template = template;  
}
public void saveMahasiswa(Mahasiswa m){
    template.save(m);
}
}

这是 mahasiswa.hbm.xml:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 30, 2021 6:57:57 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entity.Mahasiswa" table="mahasiswa" catalog="kampus" optimistic-lock="version">
    <id name="npm" type="string">
        <column name="npm" length="13" />
        <generator class="assigned" />
    </id>
    <property name="nama" type="string">
        <column name="nama" length="100" not-null="true" />
    </property>
    <property name="jurusan" type="string">
        <column name="jurusan" length="100" not-null="true" />
    </property>
    <property name="alamat" type="string">
        <column name="alamat" length="100" not-null="true" />
    </property>
</class>
   </hibernate-mapping>

这是 main.java :

public class test06 {
public static void main(String[] args) {
    Resource r=new ClassPathResource("applicationContext.xml");  
BeanFactory factory=new XmlBeanFactory(r);  
  
MahasiswaDao dao=(MahasiswaDao)factory.getBean("dao");  
  
Mahasiswa m=new Mahasiswa();  
m.setNpm("1");  
m.setNama("test");  
m.setJurusan("");
m.setAlamat("");
  
dao.saveMahasiswa(m); 
}
}

谢谢

【问题讨论】:

    标签: java spring spring-boot hibernate spring-mvc


    【解决方案1】:

    您缺少classpath 上的org.hibernate.cache.Provider 类。

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

    org.hibernate.cache.Provider 在更改中被删除 休眠 3 到休眠 4.

    您使用的是 Hibernate 4,但您在 applicationContext.xml 中使用的 Spring 中与 Hibernate 相关的类来自 org.springframework.orm.hibernate3 包,因此它们需要缺少的依赖项。

    尝试使用 org.springframework.orm.hibernate4 包中的 HibernateTemplate 和 LocalSessionFactoryBean。

    如果 Spring 3.2.7 中没有这样的包,请升级到 Spring 4。

    【讨论】:

    • 感谢您的回复。。但我收到了新错误,我尝试搜索并修复它,但我无法帮助我解决错误:读取中不允许写入操作- only 模式 (FlushMode.MANUAL):将 Session 转换为 FlushMode.COMMIT/AUTO 或从事务定义中删除“readOnly”标记。
    • 我用 template.setCheckWriteOperations(false) 修复了新错误;在类 MahasiswaDao 上的方法 saveMahasiswa 上,它工作正常.. 但是我尝试在 main 方法上保存的数据没有插入到数据库中。有谁知道为什么??因为今年春天我还是个新手,还在冬眠
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 2015-05-28
    • 2018-01-25
    • 2013-10-06
    • 2012-02-28
    • 2016-06-24
    相关资源
    最近更新 更多