dinsdag 16 februari 2010

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();
  }
}

Geen opmerkingen:

Een reactie plaatsen