Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@CreationTimestamp and @UpdateTimestamp

This is my blog 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;
}


}

timestamps createdOn and updatedOn are stored successfully when new blog is created but when existing blog is updated updatedOn field is updated whereas createdOn field becomes null. I want createdOn field to retain the timestamp on which it is created. Can someone help out ?

like image 661
Hridesh Sukumar. N.H. Avatar asked Oct 03 '16 19:10

Hridesh Sukumar. N.H.


1 Answers

Add updateable=false annotation to the field , whom you don't want to update.

Below are the sample code for createddate (only insertable) and modifieddate (insertable/updateable)

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

@Column(name = "ModifiedDate")
@UpdateTimestamp
private Timestamp modifiedDate;
like image 120
Nirmal Avatar answered Nov 18 '22 05:11

Nirmal