Get Hibernate Envers Audit Data#
Get Hibernate Envers Audit Data#
- In the section Hibnerate Envers With JPA, we know how to integrate hibernate envers with jpa for storing audit data. Now, we will continue investigate the way to get the audit data from hibernate envers for using.
- So, let's assume that after we implemented hibernate envers for auditing data then the business team in company want us to export a history api that returns all changes that happened on an object which is stored in database. In this example it will be all the changes that happened with
CustomerEntity - To do so we will extract audit data of hibernate envers from database and return.
Entities#
- We will extend our entities a little bit with new two column
createdDateandupdatedDatebecause with audit data we would like to know the time that the record is created and updated. - We will update for
CustomerEntity, andItemEntitybecause they have not those fieldscreatedDateandupdatedDatebefore.
| CustomerEntity.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
| ItemEntity.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
Service#
- Now, let's create an
AuditServiceand put the implementation code as below
| AuditService.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
- The implementation above is used for query all changed that happened on a customer except deleted record case by the
customerIdand sorted bycreatedDateandupdatedDatewith desc. - So, to query audit data we will use the
AuditReaderFactoryand anentityManagerto create anAuditQueryCreator. Then with thisAuditQueryCreator, we will use it to createAuditQuerythrough method.forRevisionsOfEntity(CustomerEntity.class, true, false). In which, the first parameter is the entity that we want to get, the second parameter isselectEntitiesOnly, we should put it as true to get onlyCustomerEntityand the finial one is theselectDeletedEntitiesfor getting deleted record ofCustomerEntity. - Then with
AuditQuery, we can use it to create queryCustomerEntityby columnidand sort the result list withcreatedDateandupdatedDate. Finally, we use the methodgetResultList()to end the query and return the audit list.
Controller#
- Next, we just create a simple controller with a simple api for getting audit data as below
| AuditController.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
Testing#
- Now, let's start our spring boot application, then create and update
Customerto have some records in thecustomers_audtable as below.
| id | rev | revtype | address | created_date | dob | full_name | gender | phone | updated_date | |
|---|---|---|---|---|---|---|---|---|---|---|
| 8f0e57e4-5535-494e-b4b3-c84d9b68536e | 1 | 0 | Binh Duong Province | 2022-11-23 20:38:15 | 1995-10-10 07:00:00 | abc3@gmail.com | Nguyen Minh Duc | M | 0123456789 | 2022-11-23 20:38:15 |
| 8f0e57e4-5535-494e-b4b3-c84d9b68536e | 2 | 1 | Ho Chi Minh City | 2022-11-23 20:38:15 | 1995-10-10 07:00:00 | abc5@gmail.com | Nguyen Minh Duc | M | 0123456789 | 2022-11-23 20:41:02 |
- Then let's execute the audit api from postman and you should see the result with 2 records and sorted as below.
