Skip to content
Advertisement

Spring Data JPA Native SQL Query DELETE SQL Query

I use @Modifying annotation together with @Query annotation to perform SQL DELETE query and delete the record from a database table.

@Modifying
@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);

Error:

Exception in xxx.xxx.xx with cause = ‘javax.persistence.TransactionRequiredException: Executing an update/delete query’ and exception = ‘Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query’

Advertisement

Answer

Annotate the service method with @Transactional. Your query is wrong. Try this:

@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);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement