以下是控制器(Controller)代码:
@GetMapping("/test/hello")
public String test() {
Member m = memberService.transactionTest();
return "haha";
}
以下是服务代码:
@Transactional(rollbackFor = NullPointerException.class)
public Member transactionTest() {
Member m = Member.builder()
.username("rollback")
.age(32)
.team(teamRepository.findById(1L).get())
.coach(coachRepository.findById(1L).get())
.build();
memberRepository.save(m);
exception();
return m;
}
public void exception(){
Member m = null;
m.getUsername();
}
据我所知,由于出现了RuntimeException,当它完成时应该回滚,但插入过程很顺利,所以我想知道原因。
rollbackFor
(我不确定这是否会增加默认行为或覆盖它)。 - Thomas