Newton fan 01
Newton fan 01

Reputation: 514

Hibernate @Audited: NOT_AUDITED cannot be resolved to a variable

I create hibernate entities over tables in database.

Entity A is referring entity 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 {
...

The only problem is that EntityA is marked with @Audited annotation (org.hibernate.envers.Audited), while EntityB is not.

When publishing the application, i get the following error in the stack trace:

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).

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

How can i solve this problem?

Upvotes: 1

Views: 10960

Answers (1)

cнŝdk
cнŝdk

Reputation: 32145

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

You are not using targetAuditMode properly:

@Audited(targetAuditMode = NOT_AUDITED)

Is wrong you should use RelationTargetAuditMode.NOT_AUDITED and not only NOT_AUDITED because RelationTargetAuditMode is an ENUM so to access its constants's values we use RelationTargetAuditMode.CONSTANT_NAME.

So it should be:

@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)

Documentation:

And we can see that from the Hibernate Envers - Easy Entity Auditing Configuration, where it indicates how to use the targetAuditMode property:

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

Upvotes: 10

Related Questions