Skip to content

How to choose fields for auditing with Hibernate Envers?

I’m using Hibernate Envers in Spring Boot application with Spring Data. For example, I have a base class

@MappedSuperclass
class BaseEntity {
    @Id
    private Long id;
    // some base fields
    // getter and setter
}

And class

@Entity
class Entity extends BaseEntity {
    // some fields
    // getters and setters
}

I need to audit only actions that were done and the timestamp of them. For example, when I create new Entity or update existing Entity object, I want auditing table to contain the following information: id of entity, time of action, type action. If I will just add @Audited to Entity class then all fields of Entity will be audited.

Answer

You can exclude some fields from being audited by using the @NotAudited annotation like this …

@Audited
@Entity
class User {
  ...
  @NotAudited
  private String phoneNumber;
}

EDIT: You can control the schema of the audit table and decide exactly what info to log for each revision by creating a custom revision entity like this

In essence you define a CustomRevisionEntity and CustomRevisionLister