简体   繁体   中英

Eror while creating bean on hibernate spring project

I'm new in spring and hibernate on this project I'm using.netbeans v.8.2 and I'm using library spring 3.2.7 and hibernate 4.3.x what should I do to fix the error?

The eror: Error creating bean with name 'dao' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'template' while setting bean property 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'template' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'mysessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

this is the application 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>  

this is mahasiswa.java (mahasiswa=student in English):

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;
}


}

This is 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);
}
}

this is 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>

and this is the 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); 
}
}

Thanks

You're missing the org.hibernate.cache.Provider class on the classpath .

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

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

You're using Hibernate 4, but the Hibernate-related classes from Spring you're using in applicationContext.xml are from org.springframework.orm.hibernate3 package, so they need that missing dependency.

Try using the HibernateTemplate and LocalSessionFactoryBean from the org.springframework.orm.hibernate4 package.

If there isn't such a package in Spring 3.2.7, upgrade to Spring 4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM