我在 myeclipse 中使用带有休眠 3.1 整数的 spring 3。问题是“学生”实例没有在数据库中保存、更新或删除,没有错误或异常,但其他功能如 findbyid 和所有工作正常。
这是我的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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="hibernateSession"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="StudentDAO"
class="com.myeclipse.hibernatespring.StudentDAO">
<property name="sessionFactory">
<ref bean="hibernateSession" />
</property>
</bean>
<bean id="persistenceLayer"
class="com.myeclipse.hibernatespring.PersistenceLayer"
abstract="false" lazy-init="default" autowire="default"
p:studentDAO-ref="StudentDAO">
</bean></beans>
我的persistenceLayer
班级:
package com.myeclipse.hibernatespring;
public class PersistenceLayer {
private StudentDAO studentDAO;
public StudentDAO getStudentDAO() {
return studentDAO;
}
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}
public void addStudent(Student st){
studentDAO.save(st);
}
public Student findStudentById(Integer id){
return studentDAO.findById(id);
}
}
我的buisnesslogic
班级:
package com.myeclipse.hibernatespring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BuisnessLogic {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student(4,"gaurav",67.7f);
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
PersistenceLayer pl=(PersistenceLayer) ctx.getBean("persistenceLayer");
pl.addStudent(stu);
Student st=pl.findStudentById(2);
System.out.print(st.getName());
}
}
我的休眠.cfg:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/school
</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
MySQL Connector/J
</property>
<mapping
resource="com/myeclipse/hibernatespring/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>