Issue
I have a controller method that does these two operations:
updates customer name
retrieves the updated customer from the db
customerRepository.updateCustomerName(customerId, name);
Customer updatedCustomer = customerRepository.searchCustomer(customerId);
These are the respective repository methods:
@Modifying
@Query("UPDATE Customer c SET c.name = ?2 WHERE c.customerId = ?1")
public int updateCustomerName(Long customerId, String name);
@Query("SELECT c FROM Customer c WHERE c.customerId =?1")
public Customer searchCustomer(Long customerId);
The update is working fine.
When executed, the name is correctly updated in the database.
The problem I'm facing is that the searchCustomer method is returning the Customer object with the old data (the name it had before the update).
Not sure why.
Shouldn't this code query the db again and retrieve the updated data?
Solution
Try
@Modifying(clearAutomatically = true)
To clear the cache and force a new select against the database in the next queries
And maybe you should use flushAutomatically = true
to flush entities before your query
Answered By - Tokazio
Answer Checked By - Marilyn (JavaFixing Volunteer)