@CreationTimestamp和@UpdateTimestamp

Hri*_*.H. 9 annotations hibernate jpa

这是我的博客课

@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;
}


}
Run Code Online (Sandbox Code Playgroud)

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

小智 14

向您不想更新的字段添加updateable = false批注。

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

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

@Column(name = "ModifiedDate")
@UpdateTimestamp
private Timestamp modifiedDate;
Run Code Online (Sandbox Code Playgroud)