Hridesh Sukumar. N.H.
Hridesh Sukumar. N.H.

Reputation: 161

@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 ?

Upvotes: 15

Views: 19951

Answers (2)

Gokul
Gokul

Reputation: 1

Make your column as NOT NULL for CreatedDate in your table. So that it won't pass the null value.

Upvotes: 0

Nirmal
Nirmal

Reputation: 361

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;

Upvotes: 23

Related Questions