如果JPA提示Executing an update/delete query,那是一定是因爲Service層沒有加@Transactional和再方法加@Modifying吧。
異常場景
spring-boot2+jpa執行新增修改刪除操作時:
public interface UserRepository extends JpaRepository<User , Integer>{
@Modifying
@Query(value="update User c set c.state = ?1,c.lastupdatetime = ?2 where c.user_no= ?3")
void updateUser(int state,Date nowDate,String user_no);
}
1
2
3
4
5
6
7
異常: Executing an update/delete query
解決方案
因爲jpa要求,’沒有事務支持,不能執行更新和刪除操作’。
所以反過來講,就是在Service層或者Repository層上必須加@Transactional,來代表這是一個事務級別的操作,增刪改查除了查都是事務級別的,就當這是一個規範也是ok的。