What's the difference between @NotAudited and RelationTargetAuditMode.NOT_AUDITED in Hibernate EnVers?

What's the difference between @NotAudited and RelationTargetAuditMode.NOT_AUDITED in Hibernate EnVers?

In Hibernate Envers, both @NotAudited and RelationTargetAuditMode.NOT_AUDITED are used to exclude entities or fields from being audited, but they are applied in different contexts and have different levels of granularity.

  1. @NotAudited:
    • @NotAudited is an annotation that you can apply to specific fields within your entity class to exclude those fields from being audited.
    • It allows you to mark individual fields as non-audited, meaning changes to those fields will not be tracked by Envers.
    • This annotation provides fine-grained control over auditing at the field level.

Example:

@Entity @Audited public class MyEntity { @Id @GeneratedValue private Long id; private String auditedField; @NotAudited private String nonAuditedField; // getters and setters } 

In the example above, nonAuditedField is marked with @NotAudited, so changes to this field will not be audited.

  1. RelationTargetAuditMode.NOT_AUDITED:
    • RelationTargetAuditMode is used to specify the audit mode for related entities in one-to-many and many-to-many relationships.
    • RelationTargetAuditMode.NOT_AUDITED is used to exclude related entities from being audited. When you set this mode, changes to the related entities won't be tracked by Envers.
    • It is applied at the relationship level, affecting all related entities in that relationship.

Example:

@Entity @Audited public class ParentEntity { @Id @GeneratedValue private Long id; @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List<ChildEntity> children; // getters and setters } @Entity @Audited public class ChildEntity { @Id @GeneratedValue private Long id; @ManyToOne private ParentEntity parent; // Other fields // getters and setters } 

In this example, @Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED) is applied to the children field in ParentEntity. This means that changes to the related ChildEntity instances won't be audited when changes are made to the ParentEntity.

To summarize, @NotAudited is used at the field level to exclude specific fields from auditing, while RelationTargetAuditMode.NOT_AUDITED is used at the relationship level to exclude related entities from auditing in one-to-many and many-to-many relationships.


More Tags

mysql-error-1170 sonarqube-scan xsd.exe array.prototype.map android-datepicker aws-step-functions keylistener layout-xml ora-00904 bind

More Java Questions

More Transportation Calculators

More Animal pregnancy Calculators

More Trees & Forestry Calculators

More Various Measurements Units Calculators