I have a project in which I use Hibernate
and I wanna use Envers
for logging, but I need the username of who makes the changes. I've got a Dropwizard application
and I'm using Hibernate envers
to audit.
From the docs, I know I'm supposed to implement RevisionListener
and set the username in newRevision()
. However, I have no way of passing the username to the CustomRevisionListener
. I was thinking of implementing session but then I started thinking that Hibernate session
should differ from the dropwizard session
and I want to pass this data within the hibernate session since I want to make sure a different username doesn't mix with a revision someone else is making.
I would appreciate any input into this.
Thanks.
You likely want to read up on my post here.
The general idea is always the same in that you'll want your application to store a ThreadLocal
variable that contains the pertinent information the listener needs and then access that ThreadLocal
variable within the listener callback. Its worth noting there is no threading concerns as all of Envers happens within the same thread context of the hibernate session itself.
My suggestion would be to make your ThreadLocal
variable a top level class that you set properties on so that should it need to be extended in the future or reused across implementations, you could easily do so.