简体   繁体   中英

How to get rev id right after auditing when using Hibernate Envers

I'm using Hibernate Envers for auditing. It works fine. However, I'm try to get the rev id right after modifying the data. I have CustomRevisionEntity as below:

import javax.persistence.Entity; 
import org.hibernate.envers.DefaultRevisionEntity;
import org.hibernate.envers.RevisionEntity;

@Entity
@RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity extends DefaultRevisionEntity {

  private static final long serialVersionUID = 3775550420286576001L;

  private String username;

  public String getUsername() {
      return username;
  }

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

And CustomRevisionListener:

import org.golf.repository.domain.entity.CustomRevisionEntity;
import org.hibernate.envers.RevisionListener;

public class CustomRevisionListener implements RevisionListener {

    public void newRevision(Object revisionEntity) {
        CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
        revision.setUsername("username"); // For testing
    }  
}

So, when for example update address like below, how to get the rev id?

public class TestClass {
    updateAddress(address);

    // how to get rev id here?
}

Thanks!

OK, I solved the problem by myself. Just add another attribute eg revId in the CustomRevisionEntity class. It will add a new column revid in the customrevisionentity table. And in the CustomRevisionListener class, add something like revision.setRevId(RevMapper.getRevId()); , the RevMapper is the class that contains the static attribute revId, getter and setter. So in the TestClass, you need to set the revId using RevMapper.setRevId(id);

Above is just a simple explanation, and you need to consider how the revId is generated, etc.

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