简体   繁体   中英

Hibernate envers how to add fields to audit table

What I want is to have @ModifiedBy , @LastModifiedDate , @CreatedBy and CreatedDate to be saved to all audit tables. The thing that makes it difficult is that I don't want to have those fields in my @Entity POJO. How can this be done if it even is possible?

It is, but the implementation is what differs slightly to what you said.

The problem with introducing those things in the actual audit-row itself is you run the risk of potentially having a lot of denormalized data scattered across the audit schema, especially when you think about the fact that one audit revision can encompass multiple entities.

The Envers-way to accomplish what you describe is to tackle the pieces separately.

In order to capture who performs an audit-operation, the best way to do this is to extend or provide your own implementation of the revision-entity. In this entity you'd include a column meant to store the user name or whatever user identifying value you want.

In order to populate the user name or identifying value in that entity, you'll also need to write a custom RevisionListener and specify that on the revision-entity's @RevisionEntity annotation. You can find examples in the user documentation here .

In order to get the timestamp of the revision, you'd need to fetch not only the entity but the revision-entity for that audit row too. In doing so you're able to obtain not only the timestamp the revision occured, the custom field for who made the change, but also the type of revision (ADD,MOD,DEL) so you can then branch on whether the values you're reading are the Creation vs Modification roles.

As far as I can understand your problem you just do not want those fields to be created in your POJO class, so you can create a @MappedSuperclass containing the audit-related fields and you can extend that to all entity classes later. here, for example, I am creating an abstract class called Auditable which will be extended to all the entity classes.

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
abstract class Auditable<User> {


@CreatedBy
@Column(nullable = false, updatable = false)
private String createdBy;

@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime created;

@LastModifiedBy
@Column(nullable = false)
private String modifiedBy;

@LastModifiedDate
@Column(nullable = false)
private LocalDateTime modified;



@Column(nullable = false)
@NotBlank(message = "username is required")
private String username;


public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public LocalDateTime getCreated() {
    return created;
}

public LocalDateTime getModified() {
    return modified;
}

public String getCreatedBy() {
    return createdBy;
}

public String getModifiedBy() {
    return modifiedBy;
}
}

after this, you can easily use this in all your entity classes since this is a superclass you can use it in all your entities. for example, I am creating an entity called Employee where I wish to set the auditable fields

@Entity
@Table
@EntityListeners(AuditingEntityListener.class)
public class Employee extends Auditable<String>{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id ;

@Column(nullable = false)
private String empName ;

@Column(nullable = false)
private String department ;

@Column(nullable = false)
private Integer age ;




public Integer getAge() {
    return age;
}

public void setAge(Integer age) {
    this.age = age;
}

public String getDepartment() {
    return department;
}

public void setDepartment(String department) {
    this.department = department;
}

public String getEmpName() {
    return empName;
}

public void setEmpName(String empName) {
    this.empName = empName;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}
}

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