Hantsy
Hantsy

Reputation: 9359

How to inject current user in the Hibernate rev Entity

I used Spring 3.1, JPA 2 and Spring Data JPA(Hibenrate 4.1) in my project, and used Hibernate Envers(shipped with Hibernate 4) to audit some properties, I want to store the current logged in user in the related rev table, how to implement this? Thanks.

Upvotes: 1

Views: 2159

Answers (1)

Jigar Parekh
Jigar Parekh

Reputation: 6283

you will need to create Custom Envers listner as below

public class CustomEnversListener implements RevisionListener {
@Override
    public void newRevision(Object revisionEntity) {
       CustomRevisionEntity customRevisionEntity = (customRevisionEntity)revisionEntity;
       Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
       customRevisionEntity.setUsername(authentication.getName());
    }
}

for more information please look at hibernate wiki

Upvotes: 3

Related Questions