3

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.

t0r0X
  • 4,212
  • 1
  • 38
  • 34
Khushi
  • 325
  • 1
  • 11
  • 32

3 Answers3

8

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.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • 1
    Thanks a lot for response, ok I am clear now Native SQL will not generate any event for Hibernate so not audit. Any HQL managed by Entity manager will generate audit. What about Spring data JPA dynamic queries? (for e.g. @Query(value="delete from Employee emp where emp.id = ?1 ") , whether this will create Audit ? – Khushi Feb 21 '17 at 03:02
  • I think as Spring data JPA queries will be in transaction , i should be able to audit it. Thanks. – Khushi Feb 23 '17 at 02:23
8

it is possible and it is easy! I did an example project using these technologies: Spring boot, Spring data jpa and hibernate with Envers to audit some tables with relationships.

Here is the example: https://github.com/jcalvopinam/example-envers

I hope it will be useful to you, if you have any questions, please let me know.

JUAN CALVOPINA M
  • 3,695
  • 2
  • 21
  • 37
0

Yes, there is no problem with Spring Data JPA and Hibernate Envers integration. It tracks save, update and delete operations. You only have to add @Audited annotation above your class.

piotrb
  • 53
  • 1
  • 7