How not to audit a join table and related entities

2019-03-24 12:10发布

问题:

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.

回答1:

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.