I have to auto generate timestamp when I am creating a new record and auto generate modified timestamp when I update a record.
can anybody tell me how do I implement this. I am using openJPA.
thanks in advance.
I have to auto generate timestamp when I am creating a new record and auto generate modified timestamp when I update a record.
can anybody tell me how do I implement this. I am using openJPA.
thanks in advance.
The easiest is to use @Version annotation (documentation here)
Just add the following to your entities:
@Version
private java.sql.Timestamp myTimestamp;
/// normal getters & setters here
And it will do it automatically
You can use the following code:
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date lastModificationDate;
// getters, setters
@PrePersist
void updateDates() {
if (creationDate == null) {
creationDate = new Date();
}
lastModificationDate = new Date();
}