声明式事务部分配置内容如下:<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数据却插入到了数据库中,望大虾们解答啊
解决方案 »
- 这个空指针异常实在不知道怎么解决了
- 用java代码在Linux下备份还原mysql,在线等
- 关于tomcat的问题,求救!!!
- No action instance for path /register could be created
- java webservice群 10115013
- 讨论:为什么现在大家动辄提Hibernate呢?
- Linux 中如何部署java web工程?
- 在dom里使用getXmlEncoding()出现异常请帮忙看看
- applet中单击一个按钮打开了一个窗口, 怎样在打开后修改子窗口上面的一个标签的text值
- 当我在tomcat4下运行jsp代码,关于Transformer&DOMSource的问题,头疼啊!!!
- 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;
}
});
}