对象不在数据库中保存,删除或更新,没有任何错误

时间:2013-09-20 19:41:30

标签: java spring hibernate model-view-controller

我在myeclipse中使用带有hibernate 3.1 integeration的spring 3。问题是“学生”实例没有在数据库中保存,更新或删除而没有错误或异常,但其他函数如findbyid和all都能正常工作。

这是我的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());
    }

}

我的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>

1 个答案:

答案 0 :(得分:0)

您需要配置交易。最简单的方法是使用注释驱动的事务和@Transactional注释。看看this教程。