声明式事务部分配置内容如下:<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数据却插入到了数据库中,望大虾们解答啊
<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数据却插入到了数据库中,望大虾们解答啊
解决方案 »
- 燃煤之际!!急!!!访问ip地址的方法,为啥不能自动完成?
- ajax怎么实现用户名验证啊?。
- 求精通Spring2.x-企业应用开发详解 电子书
- 如何让tomcat不编译网页?
- Cannot create iterator for this collection
- 请问各位高手,介绍一下J2EE应用服务器,开发一个J2EE应用服务器大概思路是什么?
- 求教Hibernate中xml配置文件字段数据类型错误
- 同一servlet中的init方法在多个用户请求时是不是只有第一个用户执行这个方法,其他用户直接执行service方法??
- ErrorsTag.doStartTag这个是版本问题吗?
- jdbc连sql server2000的速度问题?100分
- jsp页面导出成word文档
- eatj 数据库乱码
方法是可以执行的所以插进去了,也就是说是有值的,而s1.setCourses(courses);
当你要插入这个属性时,程序运行发现里面有NULL,所以报错,AAA可以插入,明白了吗。
{
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;
}
});
}