Search code examples
springhibernatejpaaudithibernate-envers

How to avoid unwanted audit using hibernate envers


i've spring and hibernate project in that i configured audit for Table and its working fine but my problem is i want to avoid audit at time of creating new record but while doing updating it should audit below my code

Entity:

@Entity
@Table(name = "building")
@Audited
public class BuildingClass extends CommonTableFields {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "BID")
   @JsonProperty
   private long id;

    @JsonProperty
    private String username;

    @JsonProperty
    private double count;
    //getters and setters
}

implementation: // here i don't want to audit

@Override   
public void save(BuildingClass buildingclass) {
       repo.save(buildingclass)
}

// here i want to audit

@Override   
public void update(BuildingClass buildingclass) {
       repo.save(buildingclass)
}

thanks in advance


Solution

  • It is going to depend on what strategy your using for auditing.

    The DefaultAuditStrategy should work by simply not registering post-insert event listener. You would do this by registering your own envers integrator that doesn't register that specific event handler.

    The ValidityAuditStrategy will be a bit of a problem. The problem here is that this strategy performs a set of update operations internally when a row is modified and those operations expect the initial insert audit row to exist and will fault if it does not.

    You could override this strategy with a custom one that disables this check, but understand the check was added to detect data issues with the audit rows rather than using assumptions.

    But the key to all this is conditional auditing, see reference documentation for information.