jkfe
jkfe

Reputation: 781

JPQL SELECT not retrieving the updated data

I have a controller method that does these two operations:

  1. updates customer name

  2. 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?

Upvotes: 0

Views: 160

Answers (1)

Tokazio
Tokazio

Reputation: 530

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

Upvotes: 2

Related Questions