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 {
publi开发者_如何转开发c 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 e.g. 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.
精彩评论