Created
November 16, 2010 15:26
-
-
Save orefalo/701927 to your computer and use it in GitHub Desktop.
DB independent and automatic update of create and update date fields - JPA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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