I have written an envers query as below;
AuditReader reader = AuditReaderFactory.get(entityManager);
List<Object[]> changes = reader.createQuery()
.forRevisionsOfEntity(cls, true, true)
// Various conditions
.getResultList();
What I would like to do is get the results in pages as can be done in PagingAndSortingRepository<T,ID>
, I have this nearly working as shown below
int start = page.getOffset();
int end = (start + page.getPageSize()) > history.size() ? history.size() : (start + page.getPageSize());
Page page = new PageImpl<HistoryEntry>(history.subList(start, end), page, history.size());
My problem is that when done this way I can't sort the data as expected i.e. size=2&sort=action,desc
does not sort no matter what value I use (asc/desc).
history
is a list of objects with the following fields;
private String timestamp;
private String userName;
private String objectName;
private String objectType;
private String action;
private String field;
private String oldValue;
private String newValue;
In order to implement paging, filtering and sorting through envers would this need to be done through the original envers query or could paging look after this?
It appears that the PageImpl
class expects the List<T>
you provide in the constructor to be the subset of the rows for the given page, already pre-sorted. Therefore, we'll need to apply the sort operations as a part of the Envers query.
List<Object[]> changes = reader.createQuery()
.forRevisionsOfEntity( cls, true, false )
// various predicate conditions
.addOrder( AuditEntity.property( "someField" ).desc() )
.getResultList();
Apply the sort this way will then allow the code where you selected the subset for the page from the results to work as intended.