Hibernate @Audited:NOT_AUDITED 无法解析为变量

Hibernate @Audited: NOT_AUDITED cannot be resolved to a variable

我在数据库中的表上创建休眠实体。

实体 A 引用实体 B

@Entity
@Table(name="TABLE_A")
@NamedQuery(.. query="SELECT n FROM EntityA n")
public class EntityA {
....
@ManyToOne(...)
@JoinColumn(...)
private EntityB b;


@Entity
@Table(name ="TABLE_B")
@NamedQuery(.. query="SELECT n FROM EntityB n")
public class EntityB {
...

唯一的问题是 EntityA 带有 @Audited 注释 (org.hibernate.envers.Audited),而 EntityB 没有。

发布应用程序时,堆栈跟踪中出现以下错误:

Caused by: org.hibernate.MappingException: An audited relation from EntityA to a not audited entity EntityB ! Such mapping is possible, but has to be explicitly defined using @Audited(targetAuditMode = NOT_AUDITED).

如果我在 private EntityB b 上方添加 @Audited(targetAuditMode = NOT_AUDITED),Eclipse 会出现以下错误

NOT_AUDITED 无法解析为变量

我该如何解决这个问题?

If i add @Audited(targetAuditMode = NOT_AUDITED) above private EntityB b, Eclipse gives me the following error

NOT_AUDITED cannot be resolved to a variable

您没有正确使用 targetAuditMode

@Audited(targetAuditMode = NOT_AUDITED)

错误你应该使用 RelationTargetAuditMode.NOT_AUDITED 而不仅仅是 NOT_AUDITED 因为 RelationTargetAuditMode 是一个 ENUM 所以要访问它的常量值我们使用 RelationTargetAuditMode.CONSTANT_NAME.

所以应该是:

@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)

文档:

而我们可以从Hibernate Envers - Easy Entity Auditing Configuration中看出,这里说明了如何使用targetAuditMode 属性:

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