title: Criteria Builder select from select
body: <p>Can anybody help me to create JPA Criteria Builder query in order to achieve this ?:</p>
  
  <pre><code>select id from (
    select distinct r.id
                    r.date
                    r.name
    from report r
    inner join unit u
          on u.report_id = r.id
    order by 
          r.date desc,
          r.name asc)
  where rownum &lt;= 10    
  </code></pre>
  
  <p>I just can create inner query:</p>
  
  <pre><code>CriteriaQuery&lt;Object[]&gt; innerQuery = cb.createQuery(Object[].class);
  Root&lt;ReportEntity&gt; root = innerQuery.from(ReportEntity.class);
  
  List&lt;Ojbect[]&gt; resultLsit = em.createQuery(
  innerQuery.multiselect(root.get(ReportEntity_.id),
                         root.get(ReportEntity_.date),
                         root.get(ReportEntity_.name)
                         .distinct(true)
                         .orderBy(cb.desc(root.get(ReportEntity_.date)),
                                  cb.asc(root.get(ReportEntity.name))
  
  ).setMaxResults(10).getResultList();
  </code></pre>
  
  <p>Thx in advance :)<br>
  I've decided to get List of Object[] and then retrieve id from array
  List idList = resultList.stream().map(array -> (Long)array[0]).collect(Collectors.toList());
  This is code smell, but unfortunatelly I haven't found better solution.
  Note I use this approach to cope Hibernate issue :</p>
  
  <p>"Warning “firstResult/maxResults specified with collection fetch; applying in memory!”? - this warning pops up due to using fetch and setMaxResults in hql or criteria query.
  That's why first of all I get all id, and then I find all entities according this id. (select * from ReportEntity r where r.id in :idList) - smth like this.</p>