Auditing Entities With JPA Events
Join the DZone community and get the full member experience.
Join For FreeThis article shows how you can leverage JPA Lifecycle Events to automate the filling in of audit information. The first example uses a base entity class with lifecycle events that looks for a time stamp interface to determine whether the entity is audited.
@MappedSuperclass public class BaseEntity implements Serializable { @PrePersist protected void onPrePersist() { populateTimestamp(); } @PreUpdate protected void onPreUpdate() { populateTimestamp(); } protected void populateTimestamp() { if (this instanceof Timestamped) { Timestamped ts = (Timestamped) this; if (ts.getCreatedOn() == null) { ts.setCreatedOn(new Date()); } else { ts.setUpdatedOn(new Date()); } } } }
The populateTimestamp method assumes that if the createdOn value isn’t set then the entity is a newly created entity. If it is set, then this is an update.
We’ve assumed that the entity with audit informatin implements a Timestamped interface that implements the get/set createdOn and updatedOn properties. This lets us implement a listener that works on any entity that implements this interface and also means that we just need to implement that interface and the auditing will be automatic.
public interface Timestamped { public Date getCreatedOn(); public void setCreatedOn(Date createdOn); public Date getUpdatedOn(); public void setUpdatedOn(Date updatedOn); }
If you don’t want to bundle the code in a common ancestor class, you can put it into a listener class.
public class EntityListener { @PrePersist public void onPrePersist(Object o) { populateTimestamp(o); } @PreUpdate public void onPreUpdate(Object o) { populateTimestamp(o); } protected void populateTimestamp(Object o) { if (o instanceof Timestamped) { Timestamped ts = (Timestamped) o; if (ts.getCreatedOn() == null) { ts.setCreatedOn(new Date()); } else { ts.setUpdatedOn(new Date()); } } } }
To specify the listener for a particular entity you specify it in the entity class :
@Entity @Listeners(EntityListener.class) public class Product implements Timestamped { }
A downside here is that you need to remember to manually add the listener as well as the Timestamped interface.
One limitation is that JPA listeners cannot have CDI beans injected into them which might be a problem. An alternative is if you are using a generic DAO class to handle entity persistence is to put the enhanced populateTimestamp method on there and call it prior to inserting or updating the entity in the DAO.
Published at DZone with permission of Andy Gibson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments