在更新CURRENT_TIMESTAMP和JPA上 [英] ON UPDATE CURRENT_TIMESTAMP and JPA

查看:343
本文介绍了在更新CURRENT_TIMESTAMP和JPA上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体字段

I have an entity with fields

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "edit_timestamp", 
        columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
private Date editTimestamp;

@Version
@Column(name = "edit_count")
private short editCount;

private String text;

当我尝试更新 Spring-Data-JPA ,我发现edit_count已经增加了,但edit_timestamp仍然保持不变。如果我手动调用SQL

When I try to update with Spring-Data-JPA, I observe edit_count has been incremented, but edit_timestamp still remain the same. If I manually invoke SQL

UPDATE post SET TEXT='456' WHERE post_id=1;

edit_timestamp已更新。如果我添加

the edit_timestamp is updated. If I add

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

它没有问题。我的问题是为什么w / o @PreUpdate的edit_timestamp没有更新?

it works w/o issue. My question is why w/o @PreUpdate the edit_timestamp is not updated?

推荐答案

这是一个老问题,如果有其他人发现它有用,请扔出答案。
我认为您需要将列注释更改为包含updatable = false。这会导致edit_timestamp列在update sql中不显示,所以JPA提供程序不会包含导致它覆盖默认值的字段的当前值。

This is an old question, but thought I'd throw in an answer in case anyone else finds it useful. I think you need to change the column annotation to include updatable = false. This will cause the edit_timestamp column to not show up in the update sql, so the JPA provider won't include the current value of the field which is what is causing it to override the default.

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "edit_timestamp", 
        updatable = false
        columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
private Date editTimestamp;

这篇关于在更新CURRENT_TIMESTAMP和JPA上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆