2

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'

1
  • is this part correct "CUSTOMERS.USER_ID = :userId and CUSTOMERS.USER_ID = :sellerId"" Commented Sep 9, 2019 at 8:53

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

Comments

1

You need a transaction to run this query. There are many ways, but the simplest one would be to annotate the service method with @Transactional. But remember it hat to be public and invoked from other bean (wrapped in proxy).

Comments

1

Method had default visibility. Therefore the proxy mechanism was not active! After changing to public everthing works as expected!

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

I use the @Transactional annotation from Spring package like @org.springframework.transaction.annotation.Transactional Then it should work

Comments

1

It's transactional problem - you need to add @Transactional. See this guide.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.