Timestamps with hibernate annotations

When you want timestamps on objects persisted with hibernate you can add callback methods that initialize these Date properties properly.

@Entity
@Table(name = "entities")
public class Entity {
//...

private Date created;
private Date updated;

@PrePersist
protected void onCreate() {
created = new Date();
}

@PreUpdate
protected void onUpdate() {
updated = new Date();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Please reload

Please Wait