Search code examples
javaspringspring-bootspring-data-jpahql

One method having two queries with insert and update with @Transactional Annotation


I have a method with @Transactional annotation used, where we are inserting a record, and at the same time we are updating the same record for one column it's not getting updated. When we checked the DB only the insert query got executed. Sample code:

@Transactional
public void testMethod(){

insert(Entity class); // inserting data in DB with id, processed, createdate column

update // updating processed column data for the above inserted record based on id

}

No error is thrown from the above method, but when I checked the DB only the insert query worked successfully but the update query didn't work.

Can you please suggest how to achieve the above requirement?


Solution

  • Updating with a query won't work as there is nothing to update in the database just yet. As you are using JPA just update the entity after your validation instead of issuing a query.