Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query

在用Spring JPA的时候,用到了@Query注解,在自定义INSERT语句的时候出现了问题:
public interface ApplicationRepository extends JpaRepository<Application, BigInteger> {
    @Modifying
    @Query(value = "INSERT INTO report(aaa,bbb) VALUES " +
            " (?1, ?2)",nativeQuery = true)
    void InsertDataIntoReport(String s1,String s2);
}

报错:Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query。

解决方案就是在该接口前加上另一个注解**@Transaction**
!!!@Transaction必须是org.springframework.transaction.annotation.Transactional,
而非javax.transaction.Transactional.

import org.springframework.transaction.annotation.Transactional;

@Transaction
public interface ApplicationRepository extends JpaRepository<Application, BigInteger> {
    @Modifying
    @Query(value = "INSERT INTO report(aaa,bbb) VALUES " +
            " (?1, ?2)",nativeQuery = true)
    void InsertDataIntoReport(String s1,String s2);
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐