【问题标题】:JPQL SELECT not retrieving the updated dataJPQL SELECT 未检索更新的数据
【发布时间】:2021-05-20 22:38:27
【问题描述】:

我有一个执行这两个操作的控制器方法:

  1. 更新客户名称

  2. 从数据库中检索更新的客户

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 对象。

不知道为什么。

这段代码不应该再次查询数据库并检索更新的数据吗?

【问题讨论】:

    标签: java hibernate jpa spring-data jpql


    【解决方案1】:

    试试 @Modifying(clearAutomatically = true) 在接下来的查询中清除缓存并强制对数据库进行新的选择

    也许您应该在查询之前使用flushAutomatically = true 刷新实体

    【讨论】:

    • 完美!这正是问题所在。刚刚接受了答案。谢谢。
    猜你喜欢
    • 2016-01-30
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多