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

时间:2016-04-15 14:26:34

标签: java spring spring-data crud transactional

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

此存储库Transactional方法如何在“引擎盖下”工作?

此外,这种方法是@Transactional吗?如果是这种情况,使用Spring-data时是否需要使用@Transactional注释。

例如这里需要public interface PersonRepository extends CrudRepository<Person, Integer> { } 吗? :

扩展CrudRepository:

 @Transactional
 public void deletePerson(Person person) {

        personRepository.delete(person);
    }

在服务类中使用delete方法:

@Transactional

修改 @Transactional public void deletePersonAndTable(Person person, Table table) { personRepository.delete(person); tableRepository.delete(Table); } 如何在这里工作?

EmpName

1 个答案:

答案 0 :(得分:6)

You don't need to add @Transactional annotations yourself.

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

Additionally, we can get rid of the @Transactional annotation for the method as the CRUD methods of the Spring Data JPA repository implementation are already annotated with @Transactional.

But you should add one in your DOA though, if you want to perform some actions sequently that should only be executed all together or none at all (that's what transactions are for).