Hibernate get old / new for audit log
I want to log the changes of some entities. so I'm using onflushdirty of hibernate interceptor.
but the previous states is always empty or null
after some search I found 2 ways to get the old/previous values:
1- open a new temp session and find the bean again
2- in order to have the previousStates not empty I have to use merge instead of save/update. (actually unsure if this is true and don't know what will happen with the actual system's behavior...)
is there any other better way to do that?
If you are just calling update with an object that you have created the previous state will be empty. You should load / get the object first and then change its properties (in our regular code not in the interceptor), on doing this you will get the previous state. Merge achieves the same thing.