title: Listing latest revision of each entity with envers
body: <p>I am trying to retrieve the latest revision of <strong>all</strong> entities, that have not been deleted. Doing this in SQL is very simple with an subselect:</p>
  
  <pre><code>select * from article_aud aud1
  where rev in
   (select max(rev) from article_aud aud2
    where aud1.id = aud2.id)
  and revtype &lt; 2
  </code></pre>
  
  <p>But I do not have a clue, how to implement this via the envers API. I started with AuditReader but did not find a way, to select distinct objects</p>
  
  <pre><code>public List&lt;Object[]&gt; findLatestArticleRevisions(){
      List&lt;Object[]&gt; results = (List&lt;Object[]&gt;) getJpaTemplate().execute(new AuditReaderCallback() {
          @Override
          public Object doInAuditReader(AuditReader auditReader) {
              return auditReader.createQuery().forRevisionsOfEntity(Article.class, false, false)
                  // TODO select distinct on entities
                  .addOrder(new PropertyAuditOrder(new RevisionNumberPropertyName(), false))
                  .getResultList();
          }
      });
  
      return results;
  }
  </code></pre>
  
  <p><strong>Important</strong>: I want to do this in one or at least two queries, because I have got many articles (entities) with many revisions.</p>
  
  <p>Thanks a lot!</p>