I`m looking for an elegant way to achieve the next task:
I have one entity called Ticket which has some fields like : owner, createdTs,body…. , and also a List entity which will keep track of all changes performed on a ticket.
My app is build with Strus2, Spring 3, Jpa + Hibernate, and i have a service class TicketService which has the method save/updateTicket(Ticket ticket).
I have tried to to implement an aspect @Before save/update method but my problem is that i cannot access the old value of ticket in order to detect changes and create TicketHistory object according to these changes.
I tried the aspect approach because i access this method from many other methods and right now these objects (Ticket and TicketHistory) are tight coupled.
any idea ?
Thanks,
Adrian
Since you’re using Hibernate, there is a very nice declarative solution:
Envers
Envers (for En titiy Vers ions) automatically detects changes on entities annotated with
@Auditedand creates a history in the database. You can even add custom information to the revisions like the user who made the change.See the Quickstart for Envers for more information.
The downside is that you have to change some amount of code since there will be no need for
TicketHistoryanymore. Older revisions of entities are accessed via the AuditReader class.Also note that when using Envers, your program depends directly on Hibernate, and not just JPA anymore. If you ever want to change your persistence provider (not that this happens very often), you’re gonna have some more work to do.