We have a requirement on our project, where we need to maintain a sort of history of changes that are made to certain Entities in the Application. The the Application is a Java Web App based on Struts, Spring and Hibernate. What sort of approaches have been used in this case ?
Are there any other approaches for doing this sort of activity ?
JBoss Envers supports audition and versioning - take a look.
Because you said
All edits do not have their corresponding different methods... many edits happen in a single java Method
Even if your method just receive one argument as parameter and you can retrieve its relationships, AOP still can be a nice idea. I strongly advice you to see this nice article
The Hibernate documentation itself says:
Hibernate Interceptor allows the application to inspect and/or manipulate properties of a persistent object before it is saved, updated, deleted or loaded. One possible use for this is to track auditing information.
But it also says
Ensure that you do not store sessionspecific states, since multiple sessions will use this interceptor potentially concurrently
If you use a Spring managed Transaction, you can get the session atached to the current Thread by using sessionFactory.getCurrentSession();
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
if(entity instanceof SomeEntity) {
Session session = sessionFactory.getCurrentSession();
See getCurrentSession() documentation
If not, you should create a local Hibernate interceptor (attached to the opened Session) instead of a global interceptor(attached to the SessionFactory), and then, set up its session
AuditableInterceptor interceptor = new AuditableInterceptor()
Session session = sessionFactory.openSession(interceptor);
/**
* do it before processing anything if you use local interceptor
*/
interceptor.setSession(session);
About Triggers, it depends on other issues like DB administration (you have DB administration access. If not, Talk to DBA is your best bet).
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