简体   繁体   English

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

[英]Is the CrudRepository .delete() method transactional?

When using Spring-data it is possible to extend the CrudRepository . 使用Spring-data时,可以扩展CrudRepository

How does this Repositories .delete() method work "under the hood"? 这个Repositories .delete()方法如何在“引擎盖下”工作?

Also, is this method Transactional ? 此外,这种方法是Transactional吗? If this is the case, is there any need to use @Transactional annotations when using Spring-data . 如果是这种情况,在使用Spring-data时是否需要使用@Transactional注释。

eg is @Transactional needed here? 例如,这里需要@Transactional吗? :

Extending CrudRepository: 扩展CrudRepository:

public interface PersonRepository extends CrudRepository<Person, Integer> {

}

Using delete method in service class: 在服务类中使用delete方法:

 @Transactional
 public void deletePerson(Person person) {

        personRepository.delete(person);
    }

Edit: How would @Transactional work here? 编辑: @Transactional如何在这里工作?

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

            personRepository.delete(person);

            tableRepository.delete(Table);

        }

You don't need to add @Transactional annotations yourself. 您不需要自己添加@Transactional注释。

From https://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/ : 来自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. 此外,我们可以删除方法的@Transactional注释,因为Spring Data JPA存储库实现的CRUD方法已经使用@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). 但是你应该在你的DOA中添加一个,如果你想要执行一些只能一起执行或根本不执行的操作(这就是交易的目的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM