Find max revision of each entity less than or equal to given revision with envers

前端 未结 2 1930
醉梦人生
醉梦人生 2020-12-11 10:41

This might be simple, but unable to find a way. I am trying to find max revision of each entity less than or equal to given revision number.

AuditQuery query         


        
相关标签:
2条回答
  • 2020-12-11 10:54

    I think what you are looking for the AuditEntitry.revisionNumber.max() constraint. This should maximize the revision number satisfying the nested constraints.

    See also the documentation on that matter.

    0 讨论(0)
  • 2020-12-11 10:56

    I was looking for something similar but found a much better way to get what I wanted. It's taken a while to work this out today, but it makes sense now!

    In my case I wanted to get entities at a particular date in the past. In Envers this means at a particular revision, as the revision is universal across all entities, not per entity.

    So, the idea is to find the revision number from the date, and then fetch all entities at that revision. I think this is similar to what you are after.

    AuditReader reader = AuditReaderFactory.get(this.entityManager);
    
    Number latestRevAtDate = reader.getRevisionNumberForDate(convertedRunDate);
    
    reader.createQuery()
          .forEntitiesAtRevision(MyEntity.class, latestRevAtDate)
          .add(AuditEntity.property("someEntityProperty").le("someValue"))
          .getResultList()
    

    You only the individual entities instead of multiple revisions of each entity, and you get all entities that were in the system at the time of that revision.

    This post helped a bit: https://developer.jboss.org/message/625074#625074

    0 讨论(0)
提交回复
热议问题