On 07/07/2011 12:05, Bengt Rodehav wrote:
I'm using OpenJPA for persistence and would like to audit log any changes
made to my entities. I serialize the objects to JSON (with Gson) and store
them in a separate table in the database. Since the audit log needs to have
the correct id's, the audit logging must take place after the entity has
been persisted.

I was hoping I could use the @PostPersist and @PostUpdate life cycle
callbacks for this.
A bit late to the party, but this is what I've got working:

Change the entity ID column so it's not allocated by the database, then it becomes valid from the time the entity is persisted, rather than the time it's flushed. By using a table type sequence generator the disadvantages relative to a database generated ID are few (and IME are only seen if you are abusing the IDs :) )

I only carry out an audit in @PreUpdate, because all I track is changes and there aren't any changes for a new record - so actually the ID generation should be irrelevant. But by working in PreUpdate I've found that I am able to work with the entity manager and create new entities for the audit.

The entity being audited has a OneToMany join to the audit entries, so I don't have to call persist on the audit log object, I just add it to the collection in the entity being audited.

Jim

Reply via email to