Hibernate Envers get revisions for criteria

守給你的承諾、 提交于 2019-12-25 02:38:59

问题


Using Hibernate Envers (4.1.9.Final). Trying to get all the revisions (date, revision number) for which a entities have changed of a certain type and that match a certain criterion.

This is the code that I'm currently having:

    AuditReader auditReader = AuditReaderFactory.get(entityManager);
    AuditQuery query = auditReader.createQuery()
            .forRevisionsOfEntity(InventoryItem.class, false, true)
            .add(AuditEntity.property("section_uuid").eq(sectionUuid))
            .addOrder(AuditEntity.revisionNumber().desc());
    List<Object[]> revisions = query.getResultList();
  1. This returns one element for each changed InventoryItem. So, if two InventoryItems were changed in a revision, I get two elements -- I do not want that.
  2. This returns also the actual InventoryItems, I think that's a bit heavy -- I do not want that.

How can I get a distinct collection of revisions (date, revision number)?


回答1:


I think what you are looking for is the addProjection method on AuditQuery. You can add a projection on the revision number and date.




回答2:


On basis of Adam's answer, here is the code that I implemented. I'll mark his answer as the accepted answer.

AuditReader auditReader = AuditReaderFactory.get(entityManager);
AuditQuery query = auditReader.createQuery()
    .forRevisionsOfEntity(InventoryItem.class, false, true)
    .addProjection(AuditEntity.revisionNumber().distinct())
    .addProjection(AuditEntity.revisionProperty("created"))
    .add(AuditEntity.property("section_uuid").eq(sectionUuid))
    .addOrder(AuditEntity.revisionNumber().desc());

As a result, query.getResultList() will return a collection of Object[], where each Object[] contains:

  • Object[0]: revision number as int
  • Object[1]: revision date as java.util.Date, corresponding the created revisionProperty


来源:https://stackoverflow.com/questions/25542231/hibernate-envers-get-revisions-for-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!