Setting Auditing Columns using JPA Listeners

This tutorial explains the using JPA Listeners  such as @prePersist, @preUpdate, @preRemove for setting Auditing Columns of a Table.

For Project Setup, Database Table Creation, Project Structure and JPA Hibernate Configuration please refer the below tutorial

CRUD Operations using JPA on Hibernate, MySQL and Maven

JPA provides the below powerful Listeners

    1. @PrePersist This listener is called just before a JPA Entity is being Persisted (Inserted) to the Database.
    2. @PreUpdate  This listener is called just before a JPA Entity is being Merged (Updated) in the Database.
    3. @PreRemove  This listener is called just before a JPA Entity is being Removed (Deleted) from the Database.

Let us consider a Use Case to make use of the above Listeners in Real World Application.

Consider the below EMPLOYEE Table which we used in our previous example

EMPOYEE Table

EMPOYEE Table

For this example lets us consider the below 4 Auditing Columns for our example.

    1. CREATED_BY     This column contains the recent most User Id of the User who created this record.
    2. CREATED_DATE  This column contains the Timestamp as to when this record was created.
    3. MODIFIED_BY   This column contains the recent most User Id of the User who modified this record.
    4. MODIFIED_DATE This column contains the recent most Timestamp as to when this record was modified.

Since the above columns are part of all the Tables in a Database Schema, hence in Object context it is sensible to move them to
the Base Class and then let all our JPA Entities extend it. By doing this we are removing the Boilerplate Code of setting these fields in
our specific Entity and move them to one place.

Let us create our Base Class BaseEntity.java as below

To let JPA know this is a Super Class we annotate this class with @MappedSuperClass else JPA would throw an error saying Could No Find Table for this Entity.

Now our we shall remove the Fields (createdBy, createdDate, modifiedBy, modifiedDate) and their Setters & Getters from our Employee Entity and after extending BaseEntity it would look something like this.

Also if we had another Entity say Department it would also extend BaseEntity and would look like this

Let us add the above mentioned Listeners to BaseEntity

In the above listener method we set all the 4 fields before Persisting an Entity.

In the above listener method we set all the 3 fields before Updating an Entity.

In the above listener method we just sysout before Removing an Entity.

Now our BaseEntity would look something like this.

Let us code JPAMain.java in order to test the above Listeners.

Output

Output

prePersist

 

Output

preUpdate

 

preRemove

preRemove

Download JPA Listeners Project

I hope this has been useful for you and I’d like to thank you for reading. If you like this article, please leave a helpful comment and share it with your friends.

Leave a Reply

Your email address will not be published. Required fields are marked *