Thanks Pinaki - it worked fine.

I now use

*  @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
> **  @JoinTable(name = "entity_alog", joinColumns = @JoinColumn(name =
> "entityId"), inverseJoinColumns = @JoinColumn(name = "ALOG_ID"))
> **  private Collection<AuditLog> auditLog;*


Another question on the same theme...

I've put the above in a MappedSuperClass that all my entities extends. That
way I can have all my standard properties (createdBy, createdWhen,
updatedBy, updatedWhen, version and now auditLog) in one place.

However, the join table between my entity and the audit log table
("entity_alog" above) only contains two columns - the id of the entity and
the id of the audit log entry. This means that differents sub classes of my
MappedSuperClass cannot share the same join table. Does that mean I cannot
use a MappedSuperClass but must use "true" inheritance with a discriminator
column?

Also, it seems I have a flaw in the design presently. 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.

And one final question...

I did mark the OneToMany as LAZY. Will that be enough? When I add an audit
log entry (by adding an element to the collection) will OpenJPA read the
rest of the audit log entries from the database ("synchronize")? That would
be a real performance hog which I really want to eliminate.

I like Jim's approach since it gives me a centralized location for all audit
log entries. The drawback is that I get a join table that I have no use
whatsoever for. Ideally, JPA/OpenJPA would allow me to create fresh entities
in the PreUpdate (or in some new callback...) without requiring a
relationship from this entity. I think this possibility is really needed for
any framework-like possibilities with OpenJPA.

/Bengt


2011/7/13 Pinaki Poddar <ppod...@apache.org>

> Hi,
>  Try setting cascade=CascadeTpe.ALL on @OneToMany annotation of
>    private Collection<AuditLog> auditLog;
>
>
>
> -----
> Pinaki Poddar
> Chair, Apache OpenJPA Project
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Audit-log-with-OpenJPA-tp6557932p6577022.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Reply via email to