java 如何不使用 Hibernate Envers 审计连接表和相关实体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11449611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:09:58  来源:igfitidea点击:

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

javahibernatehibernate-envers

提问by Romain Linsolas

I use Hibernate Envers to audit my entities.

我使用 Hibernate Envers 来审计我的实体。

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

我有一个经过审计的实体 ,Foo它有一个List<Bar>as 属性。但是,我不想审计Bar实体。因此,我写道:

@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:

现在,我想检索以下版本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:

不幸的是,当我想检索所有数据时(即当它延迟加载时bars),我收到错误ORA-00942: table or view does not exist,因为它试图查询:

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 Baritems of the currententity.

我虽然使用@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED),Hibernate Envers 会保持与当前实体的Bar项目的链接。

So how can I solve my problem, without having to explicitely audit the tables T_BARand T_FOO_BAR(the join table)? In others words, when I retrieve the list of barsfrom my revision entity, I get the list of barsfrom my current entity (as the links between Fooand Barare not audited).

那么如何解决我的问题,而不必明确地审计表T_BART_FOO_BAR(连接表)?在别人的话,当我检索列表bars从我的修订实体,我得到的名单bars,从我目前的实体(如之间的联系Foo,并Bar未经审计)。

Thanks.

谢谢。

回答by bvulaj

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

看起来您在@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)应该@NotAudited在您的情况下使用时正在使用。

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

RelationTargetAuditMode.NOT_AUDITED不会审计目标实体。它仍然会尝试审计 的List<Bar>属性Foo,从而审计连接表。

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.

如果你想审计一个关系,其中目标实体没有被审计(例如类似字典的实体就是这种情况,它不会改变也不必被审计),只需用@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED). 然后,在读取实体的历史版本时,该关系将始终指向“当前”相关实体。