Khushi
Khushi

Reputation: 333

Spring Data JPA and Spring Boot auditing with Hibernate Envers

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.

Upvotes: 3

Views: 15422

Answers (3)

JUAN CALVOPINA M
JUAN CALVOPINA M

Reputation: 3955

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.

Upvotes: 8

Naros
Naros

Reputation: 21113

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.

Upvotes: 8

piotrb
piotrb

Reputation: 53

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.

Upvotes: 0

Related Questions