技术堆栈-休眠,春季启动1.5.10,Mysql
@Component("A")
Class A {
@Transactional(rollbackFor = {Exception.class})
public void process() {
User user = new User()
dao.persist(user);
Employee e = new Employee ()
dao.persist(e);
}
Class Dao {
@Autowired
private SessionFactory sessionFactory;
public void persist(Object o) throws Exception {
sessionFactory.getCurrentSession().persist(o);
}
}
错误:
Constant violation occurred while persist the employee object.
Expected behavior should be that rollback entire DB stuff (should not persist user object).
当前,即使在持久保留雇员对象时发生约束冲突,它仍能成功插入用户记录。
我使用了方法级别的交易属性。
任何提示都会在违反约束时回滚整个事务。