DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Auditing Entities With JPA Events

Andy Gibson user avatar by
Andy Gibson
·
May. 10, 12 · Interview
Like (0)
Save
Tweet
Share
13.75K Views

Join the DZone community and get the full member experience.

Join For Free

This 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.

 

 

 

entity Event

Published at DZone with permission of Andy Gibson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 19 Most Common OpenSSL Commands for 2023
  • NoSQL vs SQL: What, Where, and How
  • Java REST API Frameworks
  • How Chat GPT-3 Changed the Life of Young DevOps Engineers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: