Search code examples
springspring-orm

Get changed fields by Spring Auditing


Can I somehow use spring Auditing to get list changed fields of entity and their old, new values?

I was trying to get the list of updated fields for entity and their old and new values for logs. I used getDeclaredFields to compare old entity and new entity before updating, and it works. But still was wondering if there are some standart and simplier ways of solution. I am using spring and hibernate to manage my entities. After researching I found that there can be used hibernate session for my purposes, by adding listener on updating event and using getDirtyProperties from event. But as I understood it requires hibernate transaction manager, and I am already using JpaTransactionManager and can not change it.

Then I was told about this spring audit feature http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/#jpa.auditing and the question is if it can provide something like getDirtyProperties? Or maybe there are another ways of solution? Because creating new tables is unwanted, and if I get it right, spring auditing requires storage audit data in data base.


Solution

  • There's actually nothing in Spring's Auditable that requires it be stored in the database, but it also doesn't include the level of detail you're looking for as far as which fields changed. Hibernate Envers does that, but I believe it can only store audit records in the database. You might be able to use it as a starting point, though.