Skip to content

Instantly share code, notes, and snippets.

@orefalo
Created November 16, 2010 15:26
Show Gist options
  • Save orefalo/701927 to your computer and use it in GitHub Desktop.
Save orefalo/701927 to your computer and use it in GitHub Desktop.
DB independent and automatic update of create and update date fields - JPA
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import play.db.jpa.Model;
@MappedSuperclass
public class TemporalModel extends Model {
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", nullable = false)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated", nullable = false)
private Date updated;
@PrePersist
protected void onCreate() {
updated = created = new Date();
}
@PreUpdate
protected void onUpdate() {
updated = new Date();
}
public Date getCreated() {
return created;
}
public Date getUpdated() {
return updated;
}
}
Usage:
@Entity
public class BuildRecord extends TemporalModel { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment