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
How not to audit a join table and related entities using Hibernate 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 Bar
entities. 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 Bar
items 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_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).
那么如何解决我的问题,而不必明确地审计表T_BAR
和T_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 @NotAudited
in your case.
看起来您在@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
应该@NotAudited
在您的情况下使用时正在使用。
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.
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)
. 然后,在读取实体的历史版本时,该关系将始终指向“当前”相关实体。