【发布时间】:2019-09-19 14:55:50
【问题描述】:
@UpdateTimestamp
@Column(name="UPDATE_TIME_STAMP")
private LocalDateTime updateTimeStamp;
@CreationTimestamp
@Column(name="POLICY_ISSUE_DATE")
private LocalDateTime policyIssueDate;
您好,我是休眠新手,我的实体和数据库中都有以上字段。但是,对于我从 SpringBoot 控制器访问它的 Dao 方法,它不会在使用以下方法执行查询更新时更新数据库中的 UPDATE_TIME_STAMP 字段。
@Override
public int updatePolicyStatus(String status,int policyNumber) {
Query query = entityManager.createQuery("UPDATE Policy p SET POLICY_STATUS=:status where POLICY_NUMBER=:policyNumber");
query.setParameter("status",status);
query.setParameter("policyNumber", policyNumber);
return query.executeUpdate();
}
此外,如果我尝试合并实体,它会尝试将 creationTimeStamp 更新为 NULL,由于我在数据库中将 POLICY_ISSUE_DATE 保持为 NOT NULL,因此最终失败。
@Override
public void updatePolicy(Policy policy) {
entityManager.merge(policy);
}
所以我如何在使用 merge(policy) 时避免 creationTimeStamp 的 NULL 值,以及为什么 updateTimeStamp 不使用 executeUpdate() 更新。
【问题讨论】:
标签: java hibernate spring-boot jpa