Springboot 1.5.7 / SpringDataJPA - 为什么默认情况下存储库中的方法并非都是事务性的
Springboot 1.5.7 / SpringDataJPA - Why methods in repository are not all transactional by default
我存储库中的这段代码:
public interface ClientRepository extends CrudRepository<Client, Long> {
//@Transactional
@Modifying
@Query("update Client c set c.firstName = ?1, c.lastName = ?2, c.email = ?3 where c.id = ?4")
void updateClientInfoById(String firstname, String lastname, String email, Long userId);
使用 REST 服务执行此方法 (updateClientInfoById) 时出现此异常:javax.persistence.TransactionRequiredException:正在执行 update/delete 查询
我必须添加@Transactional 才能使其正常工作。
为什么默认情况下存储库中的方法并非都是事务性的?
提前致谢:)
只有 CRUD 方法默认标记为事务性的。您正在使用自定义查询方法,因此您应该使用 @Transactional
注释明确标记它。
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.
有关详细信息,请参阅 Getting started with Spring Data JPA
编辑:CRUD 方法是 CrudRepository
方法
我存储库中的这段代码:
public interface ClientRepository extends CrudRepository<Client, Long> {
//@Transactional
@Modifying
@Query("update Client c set c.firstName = ?1, c.lastName = ?2, c.email = ?3 where c.id = ?4")
void updateClientInfoById(String firstname, String lastname, String email, Long userId);
使用 REST 服务执行此方法 (updateClientInfoById) 时出现此异常:javax.persistence.TransactionRequiredException:正在执行 update/delete 查询
我必须添加@Transactional 才能使其正常工作。
为什么默认情况下存储库中的方法并非都是事务性的?
提前致谢:)
只有 CRUD 方法默认标记为事务性的。您正在使用自定义查询方法,因此您应该使用 @Transactional
注释明确标记它。
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.
有关详细信息,请参阅 Getting started with Spring Data JPA
编辑:CRUD 方法是 CrudRepository
方法