Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

SptringDataJpa的Transactional注解疑惑

过程是这样:单元测试test → 调用service → 调用repository

test层
image.png

service层
image.png

repository层
image.png


请问大佬们,如何解释日志里打印的3个update记录?特别是它们执行的顺序(第二个update语句,优先set name的,应该就是repository里面我手写的update方法)。
image.png


1。本来打算3个层,都使用Transactional注解的,因为既然Transactional默认是继承上级事务,那最终其实只有一个事务在执行,但是,如果3个层都使用Transactional,会出现事务回滚。

2。搜索了一些文章,说因为hibernate的数据状态,所以查询出来的数据,set了某个字段后,在事务结束的时候会自动执行update,但是我怎么也想不明白,第三个update日志,是怎么来的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这个得了解一下 hibernate 的事务机制了,具体可以参考下这篇文章 How Spring and JPA / Hibernate Transaction Management works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...