Search code examples
hibernatespring-boothibernate-enversauditingspring-data-envers

How to fill audit fields that are in Audit extendable entity mapped super class?


I'm using hibernate envers to audit my entities. I have OfficeEntity like this:

@Entity
@Audited
@EntityListeners({AuditingEntityListener.class})
@Table(name = "office")
@AuditTable("office_aud")
public class OfficeEntity extends Auditable{

      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "office_id")
      private Long id;

      @Column(name = "office_code")
      private String officeCode;

      @Column(name = "office_name")
      private String OfficeName;

      //getters & setters & constructor 
}

and Auditable entity like this :

@MappedSuperclass
public class Auditable{

      @CreatedBy
      @Column(name = "created_by")
      private String createdBy;

      @CreatedDate
      @Column(name = "created_date")
      private Date createdDate;

      @LastModifiedBy
      @Column(name = "last_modified_by")
      private String lastModifiedBy;

      @LastModifiedDate
      @Column(name = "last_modified_date")
      private Date lastModifiedDate;

      //getters & setters & constructor
}

It seems with this structure audit fields does not fill in office_aud table. So anybody knows how can i fill audit fields in office_aud table??


Solution

  • I found annotation in hibernate envers that solved this problem. So you can add:

    @AuditOverride(forClass = Auditable.class)
    

    to the child entity class and audit fields will be set. Child entity will be like this:

    @Entity
    @Audited
    @Table(name = "office")
    @AuditOverride(forClass = Auditable.class)
    @AuditTable("office_aud")
    public class OfficeEntity extends Auditable{
    
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "office_id")
      private Long id;
    
      @Column(name = "office_code")
      private String officeCode;
    
      @Column(name = "office_name")
      private String OfficeName;
    
      //getters & setters & constructor 
    

    }