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

2

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

        <!-- 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 to some records, I noticed that the modifications are actually being audited in the table, 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?

    
asked by anonymous 08.04.2014 / 14:52

1 answer

1

According to the Hibernate Envers documentation , the date of each revision is stored in the audit tables.

/ p>

Check the revision date

You can get the date for a review using the getRevisionDate() , which receives the revision number as a parameter.

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

In short, 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 ...
}

See this article for more details.

Store the date in the table itself

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

    
08.04.2014 / 16:57