blady9565
blady9565

Reputation: 19

Hibernate Envers - pass custom field

I have problem with Hibernate Envers. How can I pass custom fields into RevisionListener? For example:

I have some entity with @Audit annotation, and everything works fine. But i have no idea how can i pass custom fields like "comment".

@Entity
@Table(name = "REVINFO")
@RevisionEntity(MyRevisionEntityListener.class)
public class RevEntity {
    @Id
    @RevisionNumber
    @Column(name = "REV", nullable = false, updatable = false)
    private Integer id;

    @RevisionTimestamp
    @Column(name = "REVTSTMP", nullable = false, updatable = false)
    private Date timestamp;

    @Column(name = "MODIFIED_BY", length = 100)
    private String modifiedBy;

    @Column(name = "COMMENT", length = 100)
    private String comment;
public class MyRevisionEntityListener implements RevisionListener {
    @Override
    public void newRevision(Object revisionEntity) {
        RevEntity a = (RevEntity) revisionEntity;
       a.setComment("");//how can i populate it?
    }
}

So, how can i set comment field?

Edit---------

I found some trick with ThreadLocal - but it is safe with multiple "active users" ?

For example:

public class CustomComment {

    public static final CustomComment INSTANCE = new CustomComment();

    private static final ThreadLocal<String> storage = new ThreadLocal<>();

    public void setComment(String comment) {
        storage.set(comment);
    }

    public String get() {
        return storage.get();
    }
}

And i have Service which modify entity:

CustomComment.INSTANCE.setComment("custom comment");
someRepository.save(someEntity);

Than everything works how i want. But is this solution safe? If 2 or more ppl "change" different entities, will this work good?

Upvotes: 0

Views: 1403

Answers (1)

Robert Bain
Robert Bain

Reputation: 9586

Than everything works how i want. But is this solution safe? If 2 or more ppl "change" different entities, will this work good?

If you are performing your update within a @Transaction then everything within the transaction boundary will happen in the same thread, so you're all good.

Upvotes: 1

Related Questions