How to store date/time of change in BD using Hibernate Envers?

Asked

Viewed 708 times

2

I need to keep a log of all transactions of an entity with Hibernate Envers. I set persistence.xml as below:

        <!-- Configurações do Envers -->
        <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" />

        <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener" />
        <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener" />

I created an Author class as below:

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.envers.Audited;

@Entity
@Audited
public class Autor {

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

    private String nome;

    @OneToMany(mappedBy = "autor")
    private List<Livro> livro;

    public Autor() {

    }

    public Autor(String nome) {
        this.nome = nome;
    }

    public Long getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public List<Livro> getLivro() {
        return livro;
    }

    public void setLivro(List<Livro> livro) {
        this.livro = livro;
    }

    @Override
    public String toString() {
        return "Autor [id=" + id + ", nome=" + nome + ", livro=" + livro + "]";
    }
}

Checking what happened in the Database after some insertions and changes in some records, I noticed that the modifications in the table are actually being audited, but I’m not finding a way to store the date and time of each of the modifications. Is there any annotation that can be placed in the class that records this information?

1 answer

1

According to the documentation of hibernate envers, the date of each revision is stored in the audit tables.

Refer to revision date

You can get the date of a review through the method getRevisionDate(), which receives the revision number as a parameter.

In addition, the documentation also says that it is possible to reset the table and audit fields with the annotation @RevisionEntity. The annotation @RevisionTimestamp can reset the field where the date is stored.

Briefly, you can override the default table. Example:

@Entity
@RevisionEntity(ExampleListener.class)
public class ExampleRevEntity {
    @Id
    @GeneratedValue
    @RevisionNumber
    private int id;

    @RevisionTimestamp
    private long timestamp;

    // Getters, setters, equals, hashCode ...
}

Behold this article for more details.

Store the date in the table itself

To store the date of the changes in the entity itself, you can create a date attribute and fill it with the change date through a Interceptor.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.