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:
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.TransactionalI 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
How to Update Each Element in a List in Java 8 Using Stream API
Letting a Button Blink White to Red, Then Giving Its Old, Rounded Form
How to Download a Pdf File in Chrome Using Selenium Webdriver
@Responsebody , Responseentity Spring Return Object as Json
How to Remove Padding Around Buttons in Android
I Want to Detect the End of Scroll Event
How to Print Even and Odd Position Characters of an Array of Strings in Java
How to Increase the Java Heap Size Permanently
Set Layout Width Percentage of the Total Screen Width
Find Value for a Key from a Dynamic Json Using Java
Java How to Set an Auto Increment Attribute
Pdf to Byte Array and Vice Versa
Error: Java: Invalid Target Release: 11 - Intellij Idea
How to Call a Database Function Using Spring Data Jpa
Disable Quick Settings Tile in Android
Spring Boot - How to Get Running Port and Ip Address
How to Fill Hashmap from Java Property File With Spring @Value
Spring Boot - Exception Encountered During Context Initialization. How to Know the Reason of Error