0

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

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Do not vandalize your posts. By posting on this site, you've irrevocably granted the Stack Exchange network the right to distribute that content under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/) for as long as it sees fit to do so. For alternatives to deletion, see: [I've thought better of my question; can I delete it?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) – Cody Gray - on strike Jan 06 '23 at 04:45

1 Answers1

0

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.

T J
  • 659
  • 1
  • 8
  • 18
  • thanks for your reply man.. but i get the new error, I try to search and fix it but i cant maybe u can lend me some help the error : Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. – Muhamad Rizki Nugroho Oct 09 '21 at 08:42
  • i fix the new error with template.setCheckWriteOperations(false); on method saveMahasiswa on class MahasiswaDao and it works fine.. but the data that I try to save on main method it's not inserted into the database. Does anyone knows why ?? because I'm still new on this spring and hibernate – Muhamad Rizki Nugroho Oct 09 '21 at 09:08