Conditional auditing questions...
jdot Jun 26, 2013 8:21 AMI have the following Entities set up & linked together.
@Entity
@Audited
public class ProductOrder extends Entity {
@ManyToOne
private User user;
....
@Entity
@Audited
public class Address extends Entity {
@ManyToOne
private User user;
....
@Entity
@Audited
public class User extends Entity
@OneToMany(mappedBy="user")
private List<Address> addresses = new ArrayList<Address>();
private boolean enabled = false;
....
I have then configured custom EventListeners as described in the hibernate doc to perform conditional auditing - I only want to audit an User if it is enabled.
protected static boolean shouldAudit(PostInsertEvent event) {
boolean audit = true;
Object entity = event.getEntity();
if (entity instanceof User) {
User user = (User) entity;
audit = user.isEnable();
}
else if (entity instanceof java.util.HashMap) {
System.out.println("why is the entity a HashMap?");
}
if (audit) {
System.out.println("Audit: " + entity.getClass().getSimpleName());
}
return audit;
}
1) From the logging I noticed that occasionally the "entity" associated to the event is an instance of a HashMap.
This only seems to happen when the event.getPersister().getEntityName() is some sort of _AUD entity.
It seems odd that the "entity" is a HashMap. Is this the correct behaviour?
2) In my test case I created three Users - none of the Users are enabled, then I create a single ProductOrder associated to a given User.
I was expecting to get no rows in the USER_AUD table, but this is not the case.
It appears the @ManyToOne user field on Address is causing a USER_AUD row to be inserted.
If I mark this field as @NotAudited then there is still a row in the USER_AUD table... from the @ManyToOne user field on ProductOrder. Only if I mark both user fields as @NotAudited do I get no User auditing.
It seems only the "outermost" entity is checked by the conditional auditing and contained references bypass any conditional checks. Is this correct behaviour?
thanks in advance
JDot