问题描述
关于Spring JPA存储库事务性的一个快速问题.我有一个未标记为事务性的服务,并调用了Spring JPA存储库方法
1 quick question on Spring JPA repositories transactionality.I have a service that is not marked as transactional and calls Spring JPA repository method
userRegistrationRepository.deleteByEmail(email);
它被定义为
@Repository
public interface UserRegistrationRepository extends JpaRepository<UserRegistration, Long> {
UserRegistration findByEmail(String email);
void deleteByEmail(String email);
}
问题是它失败,并显示"没有可用于当前线程的实际事务的EntityManager-无法可靠地处理'remove'调用;嵌套异常为javax.persistence.TransactionRequiredException ".
The problem is that it fails with "No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException" exception.
好吧,我可以通过将服务或 deleteByEmail(..)方法标记为事务性方法来解决此问题,但是我不明白为什么它现在会崩溃. Spring文档明确指出默认情况下,存储库实例上的 CRUD方法是事务性的."( http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions ),但显然这是不是...所以这条语句仅与CrudRepository
的成员有关吗?
Ok, I can solve it by marking the service or deleteByEmail(..) method as transactional, but I just can't understand why it crashes now. Spring documentation explicitly states that "CRUD methods on repository instances are transactional by default." (http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#transactions), but apparently this one is not... So Is this statement related to only members of CrudRepository
?
ps:用于Spring Data JPA 1.9.4
ps: that's for Spring Data JPA 1.9.4
推荐答案
您是对的.默认情况下,只有CRUD方法(CrudRepository
方法)被标记为事务性的.如果使用自定义查询方法,则应使用@Transactional
批注明确标记它.
You are right. Only CRUD methods (CrudRepository
methods) are by default marked as transactional.If you are using custom query methods you should explicitly mark it with @Transactional
annotation.
@Repository
public interface UserRegistrationRepository extends JpaRepository<UserRegistration, Long> {
UserRegistration findByEmail(String email);
@Transactional
void deleteByEmail(String email);
}
您还应该注意标记存储库接口方法而不是服务方法的后果.如果使用默认事务传播配置(Propagation.REQUIRED
),则:
You should also be aware about consequences of marking repository interface methods instead of service methods. If you are using default transaction propagation configuration (Propagation.REQUIRED
) then:
http://docs.spring.io /spring-data/jpa/docs/current/reference/html/#transactions
如果您想了解有关其实现方式的更多信息,请查看默认的CrudRepository
/JpaRepository
实现-SimpleJpaRepository
(您可能正在使用):
If you want more information about how it is implemented, take a look at default CrudRepository
/ JpaRepository
implementation - SimpleJpaRepository
(which you are probably using):
有趣的行在这里:
@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID> {
和此处的一些事务处理方法:
and some of transactional methods here:
@Transactional
public void deleteById(ID id) {
@Transactional
public <S extends T> S save(S entity) {
这篇关于Spring JPA存储库事务性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!