0

I already made triggers to log the actions before persisting, updating and deleting the mapped entities, but it's only within MySQL, so I think I must do an "application-level trigger" using annotations @PostPersist, @PostUpdate and @PostDelete.

So, when entity e.g. Category gets persisted, a method for inserting info into a log table is thrown, with the following SQL:

INSERT INTO log (date_hour, table, id_tuple, user) 
VALUES (NOW(), 'category', " + id + ", '" + 
FacesContext.getCurrentInstance().getExternalContext().getRemoteUser() + "')";

I made exactly like that, using createNativeQuery then query.executeUpdate() but nothing happened.

What's the best approach for doing what I want? Reminding that I'm using EclipseLink.

Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43
Rasshu
  • 1,764
  • 6
  • 22
  • 53

2 Answers2

2

You can log all changes to an entity type with little effort using EclipseLink's History policy: http://wiki.eclipse.org/EclipseLink/Examples/JPA/History

Shaun Smith
  • 171
  • 4
1

Assuming you are CDI you can create an interceptor like described in: Oracle Tutorial CDI Interceptor

In this interceptor you can create your insert in the log table. But keep in mind that logging usually slows an application down much. Consider using loglevels that you normally just log errors instead of everything

Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43