CrudRepository .delete()方法是事务性的吗?

jav*_*999 3 java spring crud transactional spring-data

使用Spring-data时,可以扩展CrudRepository.

这个存储库.delete()方法如何在"引擎盖下"工作?

还有,这个方法Transactional呢?如果是这种情况,使用@Transactional时是否需要使用注释Spring-data.

例如,在@Transactional这里需要的?:

扩展CrudRepository:

public interface PersonRepository extends CrudRepository<Person, Integer> {

}
Run Code Online (Sandbox Code Playgroud)

在服务类中使用delete方法:

 @Transactional
 public void deletePerson(Person person) {

        personRepository.delete(person);
    }
Run Code Online (Sandbox Code Playgroud)

编辑:怎么会@Transactional在这里工作?

 @Transactional
     public void deletePersonAndTable(Person person, Table table) {

            personRepository.delete(person);

            tableRepository.delete(Table);

        }
Run Code Online (Sandbox Code Playgroud)

Sim*_*mon 6

您不需要自己添加@Transactional注释.

来自https://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/:

此外,我们可以删除方法的@Transactional注释,因为Spring Data JPA存储库实现的CRUD方法已经使用@Transactional注释.

但是你应该在你的DOA中添加一个,如果你想要执行一些只能一起执行或根本不执行的操作(这就是交易的目的).