Spring Data JPA and Spring Boot auditing with Hibernate Envers

Khushi picture Khushi · Feb 20, 2017 · Viewed 12.8k times · Source

I am using Spring Data JPA and Spring Boot for my project.

I have Audit requirement for Objects(Create/Update/Delete). I may need to get Audit revisions for particular objects too. I have checked on net that Spring Data Envers cant get revisions and doesn't track deletions?

So my Question is :

1) Can we integrate Hibernate Envers with Spring data JPA?

2) We have native queries, HQLs and Spring data JPA update/save/delete dynamic queries, so would Hibernate envers be able to track object for all?

As I am new to Auditing , please let me know about above questions.

Thank you.

Answer

Naros picture Naros · Feb 20, 2017

Can we integrate Hibernate envers with Spring data JPA?

Yes, Hibernate Envers specifically integrates directly with Hibernate ORM and since Spring Data JPA already integrates with Hibernate ORM, you get this out-of-the-box.

We have native queries, HQLs and Spring data JPA update/save/delete dynamic queries, so would Hibernate envers be able to track object for all?

As long as you're manipulating entities through the Session save/update or the EntityManager's persist/merge operations, Hibernate will raise the necessary events for Envers to track your changes.

If you are using Native SQL or JPA's CriteriaUpdate/CriteriaDelete operations to manipulate database records, then no Envers will not pickup those changes. That is because Hibernate will not raise an event for those bulk or stateless operations allowing Envers to audit those changes.