-
-
Save gigenthomas/4e1a57b1517d23b691ed to your computer and use it in GitHub Desktop.
JPA @EntityListeners example
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.Entity; | |
import javax.persistence.EntityListeners; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
import javax.persistence.Temporal; | |
import javax.persistence.TemporalType; | |
@Entity | |
@EntityListeners({ | |
CreatedAtListener.class, | |
UpdatedAtListener.class | |
}) | |
@Table(name = "articles") | |
public class Article implements Creatable, Updatable { | |
@Id | |
@GeneratedValue | |
private Integer id; | |
@Column(nullable = false) | |
private String title; | |
@Column(nullable = false) | |
private String author; | |
@Column(name = "created_at", nullable = false) | |
@Temporal(TemporalType.TIMESTAMP) | |
private Date createdAt; | |
@Column(name = "updated_at", nullable = false) | |
@Temporal(TemporalType.TIMESTAMP) | |
private Date updatedAt; | |
@Override | |
public String toString() { | |
return "Article [id=" + id + ", title=" + title + ", author=" + author | |
+ ", createdAt=" + createdAt + ", updatedAt=" + updatedAt + "]"; | |
} | |
// getters and setters ommited | |
} |
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; | |
public interface Creatable { | |
void setCreatedAt(final Date date); | |
} |
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.PrePersist; | |
public class CreatedAtListener { | |
@PrePersist | |
public void setCreatedAt(final Creatable entity) { | |
entity.setCreatedAt(new Date()); | |
} | |
} |
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
public void createAndUpdateArticleExample() { | |
final Article article = new Article(); | |
article.setAuthor("Test"); | |
article.setTitle("title"); | |
System.out.println("Before creation: " + article); | |
entityManager.persist(article); | |
System.out.println("After creation: " + article); | |
try { | |
SECONDS.sleep(2); | |
} catch (InterruptedException ex) { | |
throw new RuntimeException(ex); | |
} | |
article.setAuthor("Test2"); | |
entityManager.persist(article); | |
entityManager.flush(); | |
System.out.println("After flush: " + article); | |
} |
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
Before creation: Article [id=null, title=title, author=Test, createdAt=null, updatedAt=null] | |
After creation: Article [id=1, title=title, author=Test, createdAt=Thu Nov 24 16:36:48 NOVT 2011, updatedAt=Thu Nov 24 16:36:48 NOVT 2011] | |
After flush: Article [id=1, title=title, author=Test2, createdAt=Thu Nov 24 16:36:48 NOVT 2011, updatedAt=Thu Nov 24 16:36:50 NOVT 2011] |
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; | |
public interface Updatable { | |
void setUpdatedAt(final Date date); | |
} |
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.PrePersist; | |
import javax.persistence.PreUpdate; | |
public class UpdatedAtListener { | |
@PrePersist | |
@PreUpdate | |
public void setUpdatedAt(final Updatable entity) { | |
entity.setUpdatedAt(new Date()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment