简体   繁体   中英

Hibernate envers: RelationTargetAuditMode.NOT_AUDITED vs @NotAudited

I try to audit an entity but I don't want to audit its relationships. If I put @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) in @ManyToOne relations, this works and I don't have any exception, but when I try to use the same annotation in a @onetomany with the param mappedby defined, I have an exception that says me that I have to audit the other entity.

Example:

@Table(name = "OWNERS")
@Entity
@EntityListeners(AuditingEntityListener.class)
@Audited
public class Owner {
...
  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
  @ManyToOne(fetch=FetchType.LAZY)
  private User user;
...
  @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner" )
  private Set<Pet> pets = new HashSet<Pet>();
...
}

When you use @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) you are telling hibernate not to audit this entity but audit the relation so you hibernate will save the id of the referenced entity. Thats why Pet must be an @Audited entity.

If you do not want to store the relation at all you need to use @NotAudited

Check this Whats the difference between @NotAudited and RelationTargetAuditMode.NOT_AUDITED in Hibernate EnVers?

Well, I think you have two options here:

  1. Actually audit the entity Pet , if applicable;

  2. Use the annotation @NotAudited instead of @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) . Think about it, The audit table for Owner doesn't have to persist the Pet 's associated. If it does, use option 1.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM