Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not to audit a join table and related entities using Hibernate Envers?

I use Hibernate Envers to audit my entities.

I have one audited entity, Foo, which has a List<Bar> as properties. However, I don't want to audit the Bar entities. Thus, I wrote that:

@Entity @Audited public class Foo {      @JoinTable(name = "T_FOO_BAR", joinColumns = @JoinColumn(name = "FOO_ID"), inverseJoinColumns = @JoinColumn(name = "BAR_ID"))     @ManyToMany(cascade = PERSIST)     @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)     public List<Bar> getBars() {         return bars;     }  } 

Now, I want to retrieve a revision of Foo:

    AuditReader reader = AuditReaderFactory.get(getEntityManager());     Foo revision = (Foo) reader.createQuery().forEntitiesAtRevision(Foo.class, 42).getSingleResult(); 

Unfortunately, when I want to retrieve all the data (i.e. when it lazy loads the bars), I get the error ORA-00942: table or view does not exist, as it tried to query:

select ... from T_FOO_BAR_AUD x, T_BAR y where ... 

I though that using @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED), Hibernate Envers would keep the links with the Bar items of the current entity.

So how can I solve my problem, without having to explicitely audit the tables T_BAR and T_FOO_BAR (the join table)? In others words, when I retrieve the list of bars from my revision entity, I get the list of bars from my current entity (as the links between Foo and Bar are not audited).

Thanks.

like image 768
Romain Linsolas Avatar asked Jul 12 '12 10:07

Romain Linsolas


People also ask

How Hibernate Envers works?

Hibernate Envers provides a very simple solution for CDC (Change Data Capture). It uses the Hibernate Event system to intercept all entity state transitions and audit them. The database transaction will roll back and both the actual changes and the audit log is rolled back.

What is Hibernate Envers?

The Envers module is a core Hibernate model that works both with Hibernate and JPA. In fact, you can use Envers anywhere Hibernate works whether that is standalone, inside WildFly or JBoss AS, Spring, Grails, etc. The Envers module aims to provide an easy auditing / versioning solution for entity classes.

What is Revinfo?

REVINFO. This table stores the revision information. By default, Hibernate persists only the revision number as an integer and the creation timestamp as a long. 1.


1 Answers

It looks like you're using @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) when you should be using @NotAudited in your case.

RelationTargetAuditMode.NOT_AUDITED will simply not audit the target entity. It will still try to audit the List<Bar> property of Foo, and thus the join table.

From the docs:

If you want to audit a relation, where the target entity is not audited (that is the case for example with dictionary-like entities, which don't change and don't have to be audited), just annotate it with @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED). Then, when reading historic versions of your entity, the relation will always point to the "current" related entity.

like image 127
bvulaj Avatar answered Sep 28 '22 04:09

bvulaj