views:

43

answers:

2

In a persistent object, If i change only one field, Is it possible to find out the changed field only through Hibernate? I know I can get the entire old row and new row and compare.

+4  A: 

One option is to hook into Hibernate's event system (see docs). For example, if you registered to receive FlushEntityEvent, then you'd be informed as to which columns were changing via the getDirtyProperties method of the event.

skaffman
+4  A: 

On the basis of one of your comment, my understanding is that you are looking for an auditing solution so I suggest to check Envers which is exactly about this.

The Envers project aims to enable easy auditing/versioning of persistent classes. All that you have to do is annotate your persistent class or some of its properties, that you want to audit, with @Audited. For each audited entity, a table will be created, which will hold the history of changes made to the entity. You can then retrieve and query historical data without much effort.

Pascal Thivent
Yes I was looking for an auditing solution. But envers tells weather an audited field has changed or not and not which field changed. Also it requires separate tables to store the audit data.
Vaishak Suresh