Transactionrequiredexception Executing an Update/Delete Query

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

Given your ApplicationRepository, I am assuming that you are using Spring JPA.

The exception you are facing to is javax.persistence.TransactionRequiredException.

You have already added @Transactional annotation to the repository, but please make sure that the import which you are using for is org.springframework.transaction.annotation.Transactional, because it seems that you might be using javax.transaction.Transactional.

I also suggest to you to use @Transactional on the class/method of the repository consumer.

Spring-boot TransactionRequiredException: Executing an update/delete query

Because there are multiple TransactionManagers beans defined in the project, you have to annotate transactions of non-primary TransactionaManagers with their names. For example, in my config above, the TransactionManager name is defined as "transactionManager".
So, the Transactional annotation on methods should look line this : @Transactional("transactionManager")

Executing an update/delete query in the JQPL query

You need to wrap your statements with @Transactional. To be JPA compliant you should use javax.transaction.Transactional, although in some version combination of spring-data if javax.transaction.Transactional causes issues you can try using org.springframework.transaction.annotation.Transactional.

Keep in mind that in JPA it's all about entity state transitions. When you modify the entities hiberante updates the Persistence Context and flushes it periodically.
The order in which this happens is:

  • OrphanRemovalAction
  • AbstractEntityInsertAction
  • EntityUpdateAction
  • QueuedOperationCollectionAction
  • CollectionRemoveAction
  • CollectionUpdateAction
  • CollectionRecreateAction
  • EntityDeleteAction

So naturally inserts and updates are before deletes.
When you have code which does: delete + insert the insert will actually be performed before the delete. In such cases the correct way is the fetch and update.

Unable to delete row : TransactionRequiredException: Executing an update/delete query

You can check following things in your code:

  1. If you are using spring based transaction then make sure you import org.springframework.transaction.annotation.Transactional class for @Transactional annotation
    In your case it might be javax.transaction.Transactional

  2. I see deleteClientByClientId method in your code as static. @Transactional in spring does not have support for static methods. Make your method non static which is annotated as transactional.
    You can refer @Transactional with static method

Let me know, which option works for you.



Related Topics



Leave a reply



Submit