Java 对象未在数据库中保存、删除或更新,没有任何错误
我正在myeclipse中使用Spring3和Hibernate3.1集成。问题是“学生”实例在数据库中没有保存、更新或删除,没有错误或异常,但其他函数(如findbyid和all)可以正常工作 这是我的Java 对象未在数据库中保存、删除或更新,没有任何错误,java,spring,hibernate,model-view-controller,Java,Spring,Hibernate,Model View Controller,我正在myeclipse中使用Spring3和Hibernate3.1集成。问题是“学生”实例在数据库中没有保存、更新或删除,没有错误或异常,但其他函数(如findbyid和all)可以正常工作 这是我的applicationcontext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi
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>
我的业务逻辑
类:
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);
}
}
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());
}
}
My hibernate.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>
org.hibernate.dialogue.mysqldialogue
jdbc:mysql://localhost:3306/school
根
com.mysql.jdbc.Driver
MySQL连接器/J
您需要配置事务。最简单的方法是使用注释驱动的事务和@Transactional
注释。请看一看教程。可能是因为您的事务未提交,请在hibernate会话上发布hibernate配置或尝试flush()方法。您没有保存,而是正在检索,但我认为要保存或更新,您必须在服务层中使用@Transactional annotation或类似的东西。好的,这是我的hibernate.cfg.xml