问题描述
customerRepository.updateCustomerName(customerId,name);
Customer updatedCustomer = customerRepository.searchCustomer(customerId);
这些是各自的存储库方法:
@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);
更新工作正常。
我面临的问题是 searchCustomer 方法返回带有旧数据(更新前的名称)的 Customer 对象。
不知道为什么。
解决方法
试试
@Modifying(clearAutomatically = true)
在接下来的查询中清除缓存并强制对数据库进行新的选择
也许您应该在查询之前使用 flushAutomatically = true
来刷新实体