Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Envers: how to get the revision_type information for a given revision and a given entity class

I am using Hibernate envers 3.6.3.Final. I can audit table and I can see the _audit table is populated with the revision_number, revision_type and entity data. I am making a history page, where I want to display all revision entries, so that when user clicks a revision id, then I can display the entity data along with revision type i.e. it is added, deleted or modified. I am trying to use AuditQuery, but I am not sure how to get the revision_type information for a given revision and a given entity class. Is is possible to do in AuditQuery ?

I can get the 'RevisionType' info by writing Hibernate query. But I want to reuse any existing framework api for this. In another way, I passed different 'RevisionType' as criteria to AuditQuery (i.e. multiple query for DELETE, INSERT and UPDATE) and check if I get any result, but this is not efficient way.

like image 781
Rabi Avatar asked Dec 12 '25 12:12

Rabi


1 Answers

For what it's worth now. I had the same problem and were able to get the revision type by using the AuditQueryCreator#forRevisionsOfEntity method like this:

List<Object[]> resultList = auditReader.createQuery()
    .forRevisionsOfEntity(entityClass, entityClass.getName(), false, true)
    .add(AuditEntity.revisionNumber().eq(revision)).getResultList();

This return a list of array triplets of changes concerning the specified revision. The array triplet contains the entity, entity information and at last the revision type.

Be sure the set the selectEntitiesOnly argument of AuditQueryCreator#forRevisionsOfEntity to false. If set to true the method will return list of entity objects only.

Hibernate Envers version 4.2.2 is used here.

like image 98
thoredge Avatar answered Dec 15 '25 03:12

thoredge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!