title: JPA Criteria API: join on another query
body: <p>I am trying to build this query with JPA Criteria API</p>
  
  <pre><code>SELECT s FROM snapshot s
  INNER JOIN (
      SELECT collector_id, entity_id, MAX(timestamp) AS "timestamp"
      FROM snapshot GROUP BY collector_id, entity_id
  ) AS j ON s.TIMESTAMP = j.TIMESTAMP AND s.collector_id = j.collector_id AND s.entity_id = j.entity_id;
  </code></pre>
  
  <p>The inner select should get 3 properties to identify a snapshot, and then the outer select will get all the other properties of a snapshot based on 3 that inner select returned.</p>
  
  <p>I have success with building the inner select, but how to combine the outer select with the inner using a join?</p>
  
  <p>Or, maybe, there is a different way to construct the query itself in a way, that doesn't include a sub query...</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Similar quertion: <a href="https://stackoverflow.com/questions/27509322/jpa-criteria-api-join-with-subselect">jpa criteria-api: join with subselect</a></p>