Java Spring数据JPA本机SQL查询删除SQL查询

Java Spring数据JPA本机SQL查询删除SQL查询,java,sql,spring,spring-boot,spring-data,Java,Sql,Spring,Spring Boot,Spring Data,我使用@Modifying注释和@Query注释执行SQL DELETE查询并从数据库表中删除记录 修改 @查询(value=“从客户中删除,其中CUSTOMERS.ID=:customersId和CUSTOMERS.USER\u ID=:userId和CUSTOMERS.USER\u ID=:sellerId”,nativeQuery=true) void deleteContributeur(@Param(“customersId”)Long customersId、@Param(“user

我使用
@Modifying
注释和
@Query
注释执行
SQL DELETE
查询并从数据库表中删除记录

修改 @查询(value=“从客户中删除,其中CUSTOMERS.ID=:customersId和CUSTOMERS.USER\u ID=:userId和CUSTOMERS.USER\u ID=:sellerId”,nativeQuery=true) void deleteContributeur(@Param(“customersId”)Long customersId、@Param(“userId”)Long userId、@Param(“sellerId”)Long sellerId); 错误:

xxx.xxx.xx中的异常及其原因= 'javax.persistence.TransactionRequiredException:执行 更新/删除查询“和异常=”执行更新/删除 查询嵌套异常是 javax.persistence.TransactionRequiredException:执行 更新/删除查询'


您需要一个事务来运行此查询。有很多方法,但最简单的方法是使用
@Transactional
注释服务方法。但请记住,它必须是公共的,并从其他bean(包装在代理中)调用。

您需要一个事务来运行此查询。有很多方法,但最简单的方法是使用
@Transactional
注释服务方法。但请记住,它必须是
公共的
,并从其他bean(包装在代理中)调用。

这是事务性问题-您需要添加
@transactional
。看

这是事务性问题-您需要添加
@transactional
。看

方法具有默认可见性。因此代理机制未激活!改版后一切正常

@Modifying
@Transactional
@Query(value = "DELETE FROM CUSTOMERS  where CUSTOMERS.ID =:customersId and CUSTOMERS.USER_ID  = :userId and CUSTOMERS.USER_ID  = :sellerId", nativeQuery = true)
void deleteContributeur(@Param("customersId") Long customersId, @Param("userId") Long userId, @Param("sellerId") Long sellerId);

我使用Spring包中的@Transactional注释,如
@org.springframework.transaction.annotation.Transactional
,那么它应该可以工作方法具有默认可见性。因此代理机制未激活!改版后一切正常

@Modifying
@Transactional
@Query(value = "DELETE FROM CUSTOMERS  where CUSTOMERS.ID =:customersId and CUSTOMERS.USER_ID  = :userId and CUSTOMERS.USER_ID  = :sellerId", nativeQuery = true)
void deleteContributeur(@Param("customersId") Long customersId, @Param("userId") Long userId, @Param("sellerId") Long sellerId);

我使用Spring包中的@Transactional注释,比如
@org.springframework.transaction.annotation.Transactional
,那么它应该可以工作

@Transactional
注释服务方法。 你的问题是错误的。试试这个:

@Modifying
@Transactional
@Query(value = "DELETE FROM CUSTOMERS  where CUSTOMERS.ID =:customersId and CUSTOMERS.USER_ID  IN (:userId,:sellerId)", nativeQuery = true)
void deleteContributeur(@Param("customersId") Long customersId, @Param("userId") Long userId, @Param("sellerId") Long sellerId);

@Transactional
注释服务方法。 你的问题是错误的。试试这个:

@Modifying
@Transactional
@Query(value = "DELETE FROM CUSTOMERS  where CUSTOMERS.ID =:customersId and CUSTOMERS.USER_ID  IN (:userId,:sellerId)", nativeQuery = true)
void deleteContributeur(@Param("customersId") Long customersId, @Param("userId") Long userId, @Param("sellerId") Long sellerId);
此部分是否正确“CUSTOMERS.USER\u ID=:userId和CUSTOMERS.USER\u ID=:sellerId”此部分是否正确“CUSTOMERS.USER\u ID=:userId和CUSTOMERS.USER\u ID=:sellerId”