I need to make an audit module to my Java Web App. I use EclipseLink, not Hibernate (can't use Envers). I searched a lot some way to get the SQL or JPQL that JPA is executing, so I could log something like this:
System.out.println("User " + user + " from " + ip_address + " executed " + jpa_statement + " at " + new Date());
Actually, I'll save this info into a history database's table. So I can easily retrieve this info any time I want. That's why "SHOW SQL" parameters are not enough to me. I really need the SQL string, so I can manipulate it at my source code.
I found at JPA spec the EntityListener feature and thought it was the perfect place to put my logging code. For example, the postUpdate method could log the time the object was updated. But my problem is that I can't have the SQL the was executed.
Here is an example of what I mean:
public class AuditListener {
@PostUpdate
public void postUpdate(Object object) {
User user = (User)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
String ip_address = (User)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("ip_address");
String jpa_statement = object.getSQL();
System.out.println("User " + user + " from " + ip_address + " executed " + jpa_statement + " at " + new Date());
}
}
But "object.getSQL()" doesn't exists. So how can I get the SQL statement?
If anyone could point me into the right direction, I would appreciate!
Enabling JPA Auditing. To start, we want to enable auditing via annotation configuration. In order to do that, we add @EnableJpaAuditing on our @Configuration class: @Configuration @EnableTransactionManagement @EnableJpaRepositories @EnableJpaAuditing public class PersistenceConfig { ... }
Additionaly, there is a "REVINFO" table generated, which contains only two fields: the revision id and revision timestamp. A row is inserted into this table on each new revision, that is, on each commit of a transaction, which changes audited data.
Spring Boot JPA Audit Logging Example Spring Data helps you keep track of who created or modified an entity, as well as when it happened. To leverage this auditing functionality, you must provide auditing metadata to your entity classes, which can be defined using annotations or by implementing an interface.
The Envers module is a core Hibernate model that works both with Hibernate and JPA. In fact, you can use Envers anywhere Hibernate works whether that is standalone, inside WildFly or JBoss AS, Spring, Grails, etc. The Envers module aims to provide an easy auditing / versioning solution for entity classes.
Hibernate Envers uses the concept of revisions, similar to how version control works. Below are the high level (simple) steps to enable auditing using Envers: Add @Audited annotation to the entity (for auditing all the columns in an entity) or to specific columns in an Entity
As soon as you've created and injected your repository to another component, Spring Data will provide the implementation automatically and you're ready to add auditing functionality. 4.1. Enabling JPA Auditing To start, we want to enable auditing via annotation configuration.
Hibernate Envers With Hibernate, we could make use of Interceptors and EventListeners as well as database triggers to accomplish auditing. But the ORM framework offers Envers, a module implementing auditing and versioning of persistent classes.
Auditing With JPA JPA doesn’t explicitly contain an auditing API, but the functionality can be achieved using entity lifecycle events. 2.1. @PrePersist, @PreUpdate and @PreRemove In JPA Entity class, a method can be specified as a callback which will be invoked during a particular entity lifecycle event.
EclipseLink has full support for history tracking.
See, http://wiki.eclipse.org/EclipseLink/Examples/JPA/History
JPA events do not contain what was changed, only the object.
EclipseLink also supports DesriptorEvents (see DescriptorEventListener) which also define a postUpdate but include an ObjectChangeSet that describe the changes, you also have the UpdateObjectQuery that contains the SQL and DatabaseRecord.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With