title: How best to model Hibernate Criteria Query
body: <p>I have a pseudo sql query which looks like this:</p>
  
  <pre><code>from TableA as a 
        join fetch TableB as b
   WHERE 
      (a.id, a.revision) IN (  SELECT id, MAX (revision) FROM TableA GROUP BY id)
       and 
      (b.id, b.revision) IN (  SELECT id, MAX (revision) FROM TableB GROUP BY id);
  </code></pre>
  
  <p>and I would like to use the hibernate criteria api to do the same.</p>
  
  <p>I have a solution but it has a weakness:</p>
  
  <pre><code>final Criteria criteria = getSession().createCriteria(TableA.class, "a");
  criteria.setFetchMode("btables", FetchMode.JOIN); //$NON-NLS-1$
  criteria.add(Restrictions.sqlRestriction("({alias}.id, {alias}.revision) IN (  SELECT id, MAX (revision) FROM TableA GROUP BY id)"));
  
  Criteria bSubCriteria = criteria.createCriteria("a.btables", "b");
  bSubCriteria.add(Restrictions.sqlRestriction("({alias}.id, {alias}.revision) IN (  SELECT id, MAX (revision) FROM TableB GROUP BY id)"));
  </code></pre>
  
  <p>My problem with the solution is that I have to explicitly name the table in the sql restriction.</p>
  
  <p>Does anyone have a better way I could model this, without injecting any sql, or by getting Hibernate to resolve the name of the table?</p>