问题
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.
回答1:
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.
回答2:
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/juanca87/example-envers
I hope it will be useful to you, if you have any questions, please let me know.
回答3:
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.
来源:https://stackoverflow.com/questions/42340179/spring-data-jpa-and-spring-boot-auditing-with-hibernate-envers