java CrudRepository .delete() 方法是事务性的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36649854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is the CrudRepository .delete() method transactional?
提问by java123999
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
? If this is the case, is there any need to use @Transactional
annotations when using Spring-data
.
另外,是这个方法Transactional
吗?如果是这种情况,@Transactional
在使用Spring-data
.
e.g is @Transactional
needed here? :
例如@Transactional
,这里需要吗?:
Extending CrudRepository:
扩展 CrudRepository:
public interface PersonRepository extends CrudRepository<Person, Integer> {
}
Using delete method in service class:
在服务类中使用删除方法:
@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);
}
回答by Simon
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 中添加一个,如果你想连续执行一些只能一起执行或根本不执行的操作(这就是事务的用途)。