openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <>
Subject Re: Audit log with OpenJPA
Date Wed, 13 Jul 2011 20:14:59 GMT

> On PreUpdate I save an audit log entry describing the current entity.
> However, I guess I should
> audit log the object in its previous state not its current state -
> otherwise I need to audit log in the PreCreate as well - right? I think
> I'll have to use your solution, Pinaki, to get the previous state of the
> object since
> that's what I have to log in the PreUpdate. 

Yes. Any audit facility needs to have a snapshot of the entity when it
entered a persistence context, so at @PreUpdate or at any other time points,
it can figure out what has essentially been changed about that entity in a
transaction. Now either one can build their own mechanics to store the
original state of the entity or can use OpenJPA's own facility to access the
original state. The blog article showed the later approach.

Secondly, in my view, an audit facility should be orthogonal. The actual
domain entity need not know that it is being audited. Thereby, the domain
entity need not have an association or knowledge of an Audit object.   

Thirdly, the audit facility should allow the audit information be stored in
a separate database, in the same database or may even be logged in a file.
That is to say that persistence of audit information should be decoupled
from persistence of the domain objects. 

If you intend to store audit information as a persistent entity in the same
database as the domain entity, then the simple solution is something like
this in a domain class:

  public void audit() {
	  Audit audit = new Audit();
          // now populate audit information
          // .... some serious delta computation

          // Now get the entity manager that is managing this current domain
          OpenJPAEntityManager em = 

          // And persist the audit information in the same transaction


Pinaki Poddar
Chair, Apache OpenJPA Project
View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message