数据库更新后无法更新当前时间戳

时间:2019-02-11 20:05:23

标签: java hibernate spring-boot

我有一个表,其中有一个Date字段,该字段的默认值是当前时间戳。当我在该表上创建一行时,此方法工作正常。

当我更新该行时,我希望该时间戳会自动更新,但不会被更新。感谢任何关于我做错事的建议。

这是我的Entity类中的字段。

// Date is of type import java.util.Date;

@UpdateTimestamp // expecting this to do the auto update. 
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt; 

这是我的存储库界面上的查询。

// I don't intend to pass in current timestamp as a 3rd param for updateAt field. I expect it to just auto update to current time stamp.
@Modifying
@Query("update table as t set t.title =?1 where t.Id = ?2")
void update(String title, long id);

以上查询仅更新标题和ID,而不更新updatedAt Date字段。还尝试在Entity类下进行以下操作,这没有什么区别。

@PreUpdate
protected void onUpdate(){
    updatedAt = new Date();
}

1 个答案:

答案 0 :(得分:0)

我怀疑这是正常现象,因为@PreUpdate是JPA /休眠功能。当您仅使用查询时,您只是在调用“普通SQL”而不经历Hibernate实体生命周期。我环顾四周,发现了一些确认信息:

这里也回答了: Spring Data JPA @PreUpdate not called when update using @Query from Repository

相关问题