samjaf
samjaf

Reputation: 1053

Audit entity with envers without adding annotation

I'm using Envers for auditing changes in the database.

Now I have a class from a dependency which I'd like to audit. Since I can't edit the source code I'm unable to add a simple @Audited to the annotations.

I was surprised that it seems like there was no way to audit an entity but to add the annotation. Is there any way I can manually register the entity for auditing?

Sorry, I have the feeling the answer will be rather obvious, but I didn't find a solution so far.

Upvotes: 1

Views: 824

Answers (1)

Ricardo Vila
Ricardo Vila

Reputation: 1632

It is not possible in a direct way (see How to put envers annotations into XML Mapping Metadata(orm.xml) file). You can annotate relations of your audited classes (i. e. ClassA) with this class (i.e. ClassNotAuditable) as auditable:

@Audited
public class ClassA{

  private ClassNotAuditable instance;

  @Audited
  public ClassNotAuditable getInstance(){
    return instance;
  }

}

But Envers will ignore it because the Class to audit is not marked as auditable. There is no way to mark a Class as auditable but with anotations.

But you can try a workaround. Extend that class you want to audit with another of your own and mark it as Auditable. Rewrite references on your own classes to use this new class. Maybe this will do the trick.

@Auditable
public class NewClassAuditable extends ClassNotAuditable{ 
...
}

@Audited
public class ClassA{

  private NewClassAuditable instance;

  @Audited
  public NewClassAuditable getInstance(){
    return instance;
  }

}

Upvotes: 1

Related Questions