如何不使用Hibernate Envers审计连接表和相关实体? [英] How not to audit a join table and related entities using Hibernate Envers?
问题描述
我有一个审计实体 Foo
,它有a List< Bar>
作为属性。但是,我不想审核 Bar
个实体。因此,我写道:
@Entity
@Audited
公共类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(){
返回酒吧;
}
}
现在,我想要检索 Foo
的修订:
AuditReader reader = AuditReaderFactory.get(getEntityManager ));
Foo revision =(Foo)reader.createQuery()。forEntitiesAtRevision(Foo.class,42).getSingleResult();
不幸的是,当我想要检索所有数据时(例如,当它懒加载 bars
),我得到错误 ORA-00942:表或视图不存在
,因为它试图查询:
select ... from T_FOO_BAR_AUD x,T_BAR y where ...
我通过使用 @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
,Hibernate Envers会保持与 实体实体。
T_BAR
和 T_FOO_BAR
(连接表)?换句话说,当我从我的修订实体中检索 bars
的列表时,我从我的列表中获得 bars
的列表目前的实体(因为 Foo
和 Bar
之间的链接没有被审计)。 $
看起来您正在使用 @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
当你在你的案例中使用 @NotAudited
时。
RelationTargetAuditMode.NOT_AUDITED
不会审核目标实体。它仍然会尝试审计 Foo
的 List< Bar>
属性,从而审计连接表。
从文档:
如果您想审核关系,其中目标实体不是
审计过的(例如类似字典的实体就是这种情况,
不变,不需要审计),只需使用<$ c注释
$ c> @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)。
然后,在阅读您实体的历史版本时,关系将
始终指向当前相关实体。
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.
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.
这篇关于如何不使用Hibernate Envers审计连接表和相关实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!