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

邮差的信 提交于 2019-12-04 15:33:34

问题


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.



来源:https://stackoverflow.com/questions/11449611/how-not-to-audit-a-join-table-and-related-entities-using-hibernate-envers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!