声明式事务部分配置内容如下:<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="baseTransactionProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="register">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
逻辑代码部分:Course c1 = new Course();
c1.setName("数学");
c1.setLesson((short)30);
Course c2 = new Course();
c2.setName(null);   // 此处故意设为空,以便抛出字段不能为空异常,观察数据库事务
c2.setLesson((short)45);
Set<Course> courses = new HashSet<Course>();
courses.add(c1);
courses.add(c2);

Student s1 = new Student();
s1.setName("AAA");
s1.setSex("女");
s1.setCourses(courses);
Student s2 = new Student();
s2.setName("BBB");
s2.setSex("男");
s2.setCourses(courses);

List<Student> studentList = new ArrayList<Student>();
studentList.add(s1);
studentList.add(s2);
studentService.saveStudent(studentList);
DAO处理代码:
public void saveStudent(final List<Student> list)
{
this.getHibernateTemplate().execute(new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException, SQLException
{
SessionFactory sessionFactory = getSessionFactory();
DataSource dataSource = SessionFactoryUtils.getDataSource(sessionFactory);
Connection con = dataSource.getConnection();
con.setAutoCommit(false);

for (Iterator<Student> iterator = list.iterator(); iterator.hasNext(); )
{
Student student = iterator.next();
session.save(student);

}
session.flush();
con.commit();

return null;
}
});
}
结果是AAA数据插入到数据库了,然后由于数据库字段都设置为不能为空,所以接下来抛出了异常:DataIntegrityViolationException: not-null property references a null or transient value然后,按理说spring的事务管理会自动回滚的,课时AAA数据却插入到了数据库中,望大虾们解答啊

解决方案 »

  1.   

    呵呵,我看了下你的代码原因是这句session.save(student);你保存的是Student这个类里的属性,因为这个类的s1.setName("AAA");
    方法是可以执行的所以插进去了,也就是说是有值的,而s1.setCourses(courses);
    当你要插入这个属性时,程序运行发现里面有NULL,所以报错,AAA可以插入,明白了吗。
      

  2.   

    saveStudent这个方法是个事务操作,应该要不就完全操作成功,要不就不执行,不是这样理解吗
      

  3.   

    你这个方法里面很多是多余 的,只需要中间那个for循环就行public void saveStudent(final List<Student> list)
    {
    this.getHibernateTemplate().execute(new HibernateCallback()
    {
    public Object doInHibernate(Session session) throws HibernateException, SQLException
    {
    //SessionFactory sessionFactory = getSessionFactory();
    //DataSource dataSource = SessionFactoryUtils.getDataSource(sessionFactory);
    //Connection con = dataSource.getConnection();
    //con.setAutoCommit(false);for (Iterator<Student> iterator = list.iterator(); iterator.hasNext(); )
    {
    Student student = iterator.next();
    session.save(student);}
    //session.flush();
    //con.commit();return null;
    }
    });
    }