【发布时间】:2020-07-25 19:11:27
【问题描述】:
我正在使用 Spring Boot 数据 JPA 更新表中的列。 我的数据库是 Postgres。我正在尝试更新我的数据库中 statusCode 列的值。我将其从 1 更新为 6。 以下是相同的代码:
存储库
public interface MailRecordRepository extends JpaRepository<MailRecords, Integer> {
// Fetch the rows which are new in status
List<MailRecords> findTop100ByEmailStatusCode(int statusCode);
// Update status to in-progress
@Modifying(flushAutomatically = true, clearAutomatically = true) //, flushAutomatically = true
@Query(value = "UPDATE users.usertbl SET statusCode = :inProgressStatusCode where " +
"statusCode = 1 RETURNING *;", nativeQuery = true)
public List<MailRecords> updateRecordsToInProgress(
@Param("inProgressStatusCode") int inProgressStatusCode);
}
我正在尝试通过在 Postgres 支持的本机查询中使用 RETURNING 关键字来获取更新的行。我将查询返回的更新行分配给一个列表,并在控制台中打印它。但更新后的列表返回 statusCode 列的旧值,即 1 而不是更新后的值,即 6 以下是相同的代码:
服务类
public class MailServiceImpl implements MailService {
@Autowired
private MailRecordRepository mailRecordRepository;
@Autowired
private MailHandler mailHandler;
@Autowired
private EntityManager entityManager;
@Override
public List<MailRecords> getMailRecords() {
return (List<MailRecords>) mailRecordRepository.findAll();
}
@Override
public List<MailRecords> processEmailRecords() {
//fetch records from DB which are in new status
List<MailRecords> mailRecordsToBeSent = mailRecordRepository.findTop100ByEmailStatusCode(1);
// update the status to in-progress
//entityManager.clear();
List<MailRecords> updatedMailRecords = mailRecordRepository.updateRecordsToInProgress(6);
return updatedMailRecords;
}
如果我使用实体管理器的 clear 方法,则会反映更新的值。但我想了解为什么@Modifying(flushAutomatically = true, clearAutomatically = true) 属性没有清除缓存值?为什么我必须显式调用entityManager.clear()?
谁能纠正我的理解并指出我哪里出错了?
【问题讨论】:
-
能否请您澄清您正在比较的两个版本的代码,执行其中一个版本之前存在的数据以及两个版本的结果是什么?当前包含大量注释内容的单一版本代码令人困惑。
-
@JensSchauder 通过删除评论行并添加更多描述来更新帖子!
标签: postgresql spring-boot spring-data-jpa