我需要将hibernate与spring集成在一起,并且必须使用HibernateTemplate来执行数据库操作。 但是它抛出异常。
Exception in thread "main" java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session;
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:319)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:470)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:405)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
at com.shubham.SpringHibernateAssignment.StudentDAO.saveStudent(StudentDAO.java:19)
at com.shubham.SpringHibernateAssignment.App.main(App.java:26)
在以下代码中找到 StudentDAO.java 类的代码,该类使用HibernateTemplate执行数据库操作。
public class StudentDAO {
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void saveStudent(Student stud){
template.save(stud);
}
public Student getById(int id){
Student e=(Student)template.get(Student.class,id);
return e;
}
public List<Student> listAll(){
List<Student> list=new ArrayList<Student>();
list=template.loadAll(Student.class);
return list;
}
}
这是配置文件-applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- JDBC Database connection settings -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false"></property>
<property name="username" value="hbstudent"></property>
<property name="password" value="hbstudent"></property>
</bean>
<bean id="mysessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>student.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="d" class="com.shubham.SpringHibernateAssignment.StudentDAO">
<property name="template" ref="template"></property>
</bean>
</beans>