问题
We're implementing Envers in our project for database auditing, and have run into a snag. We don't know how to determine what user is making the change. All the examples I can find use Seam and their Component.getInstance technique.
- Glassfish 3.1.2.2
- Mojarra 2.1.13
- Hibernate 4.1.6.Final
Here's our custom Revision Entity
@Entity
@RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity {
@Id
@GeneratedValue
@RevisionNumber
private int id;
private String login;
getters/setters...
}
And out custom Revision Listener
public class CustomRevisionListener implements RevisionListener {
@Override
public void newRevision(Object revisionEntity) {
CustomRevisionEntity rev = (CustomRevisionEntity) revisionEntity;
//how do we get the FacesContext/remote user?
rev.setLogin("unknown");
}
}
We've tried injection and scoping to no avail, we also attempted this ClassLoader solution I found for something that seemed to be a similar circumstance, but getCurrentInstance() was still null;
Thanks for your help!
回答1:
adamw was correct, FacesContext.getCurrentInstance() does work in a custom RevisionListener.
We had a javax.servlet.Filter implementation attempting to handle the transaction in the doFilter() method which was for whatever reason causing FacesContext.getCurrentInstance() to return null in the RevisionListener. We removed that from the web.xml yesterday for a different reason and when I tried getCurrentInstance() without it this morning, everything works fine.
Thanks for the replies everyone, maybe this will help someone else someday.
来源:https://stackoverflow.com/questions/14609394/how-to-access-facescontext-in-hibernate-envers-without-seam