Question

I am trying to create a simple change log for inserts, deletes, and changes to my tables.

I am using hibernate's HibernateEventListener class along with the SaveOrUpdateEventListener and the DeleteEventListener.

This works great for saves and deletes. I would like to show new and old state for changes. So I followed the same pattern and created a MergeEventListener.

My problem is the original value is always null. I tried a PreUpdateListener and had the same result.

According to the hibernate docs the original object will be null of the object is detached.

My objects will always be detached based on the current architecture which is:

  1. Open one session get an object
  2. Place the object into a form backing bean
  3. Display the form to the user
  4. On Save or update click
  5. Open new session and update object

I am wondering if there is any way I can get the pre updated state of the object. My detached object is updated in the controller before the merge or preupdate event happens.

Any ideas or tips would be appreciated.

Thanks

Was it helpful?

Solution 2

I was able to solve this problem using a PreUpdateEventListener.

I looked at my Hibernate Dao objects specifically the update call. I was using update which would be detached and have the null in the oldState property.

I switched the calls to merge and now the oldState property is always populated.

OTHER TIPS

If your Database supports trigger then this can be implemented in the trigger.

In case of trigger there is no issue with session - you can always update with OLD and NEW value.

Wikipedia entry gives more details about the solution: http://en.wikipedia.org/wiki/Log_trigger

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top