默认情况下,Spring Data JPA 已禁用自动提交。
因此,如果我正在使用/扩展 CrudRepository
使用 save
保存对象方法,幕后发生了什么?将对象保存到数据库后,spring jpa 是否也提交了操作。
如果没有,我如何明确提交操作?
编辑以下“Michal Drozd”评论: (以下是针对 JpaRepository 而不是 CrudRepository)
本文:Difference Between save() and saveAndFlush() in Spring Data JPA似乎表明对于 Spring Data JPA,默认情况下自动提交为 false。
When we use the save() method, the data associated with the save operation will not be flushed to the DB unless and until an explicit call to flush() or commit() method is made.
最佳答案
save()
CrudRepository
的方法是 @Transactional
本身。因此,如果您不在另一个事务的上下文中调用它,它将创建一个并根据持久化实体的成功或失败提交或回滚它。
关于java - Spring JPA,CrudRepository 的 save 方法是否立即提交到 DB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64151939/