gpt4 book ai didi

hibernate - @CreationTimestamp 和 @UpdateTimestamp

转载 作者:行者123 更新时间:2023-12-02 21:58:51 32 4
gpt4 key购买 nike

这是我的博客类(class)

@Entity
@Component
@Table
public class Blog implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private String id;
private String name;
private String Description;

@CreationTimestamp
private Date createdOn;

@UpdateTimestamp
private Date updatedOn;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;

}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public Date getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}


}

创建新博客时,时间戳createdOn和updatedOn会成功存储,但更新现有博客时,updatedOn字段会更新,而createdOn字段会变为空。我希望 createdOn 字段保留创建它的时间戳。有人可以帮忙吗?

最佳答案

在不想更新的字段上添加 updateable=false 注解。

以下是createddate(仅可插入)和modifieddate(可插入/可更新)的示例代码

@Column(name = "CreatedDate", updatable=false)
@CreationTimestamp
private Timestamp createdDate;

@Column(name = "ModifiedDate")
@UpdateTimestamp
private Timestamp modifiedDate;

关于hibernate - @CreationTimestamp 和 @UpdateTimestamp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39838735/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com