我有一个表,其中有一个日期字段,其默认值为当前时间戳。当我在那张表上创建一行时,这个方法很好。
当我更新该行时,我期望时间戳自动更新,但它不会被更新。感谢任何关于我做错了什么的建议。
这是我的实体类中的字段。
// 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日期字段。还尝试在实体类下执行以下操作,这没有任何区别。
@PreUpdate
protected void onUpdate(){
updatedAt = new Date();
}