Hibernate Envers save Username just using servlet

588 Views Asked by At

I have a project where i use Hibernate and i wanna use Envers for logging, but i need the username of who is making the changes because a incorrect configuration can lead to financial problems so the cliente wanna know who did it.

The project use Java EE but just with servets, there is anyway to get the user from a session while envers is saving the audited entity?

The logged user stay in a session, so i need to retrieve it from there or save in another place use another strategy i don't know.

I searched alot but just found solution when using Frameworks like spring, vraptor etc

I need a solution without framework because i can't port the project to this framework, at least not now.

Thx in advance!

1

There are 1 best solutions below

2
On BEST ANSWER

The easiest solution would probably be to set the session using a filter in a ThreadLocal when the request starts, and unset it when the request completes.

You can then use the RevisionListener to get the session from the thread local and set the username on a custom revision entity.