title: Hibernate Criteria most recent date field of Collection
body: <p>I have Laboratories with a collection of states, here are my simplified Classes:</p>
  
  <pre><code>    LabState {
          private Integer id;
          private State state;
          private Date date;
          private String comments;
      }
  
      State{
          private Integer id;
          private String name;
  
      }
  
      Lab{
          private Integer id;
          private List&lt;LabState&gt; states = new ArrayList&lt;LabState&gt;();
          private Date date;
          private String name
      }
  </code></pre>
  
  <p>I want to get via Hibernate Criteria the Last State from a specific laboratory. in mySql this work fine:</p>
  
  <pre><code>SELECT ls.* FROM labs_states les INNER JOIN
      ( SELECT idLab, idLabState, MAX(date) AS lastDate FROM labs_states GROUP BY idLab)
          groupedLabs ON les.idLab = groupedLabs.idLab  AND les.date = groupedLabs.lastDate
  </code></pre>
  
  <p><strong>Edit: SearchMethod:</strong></p>
  
  <pre><code>   public List&lt;Labs&gt; search(LabSearch labSearch) {
  
              SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
              Session session = sessionFactory.openSession();
  
              Criteria criteria = session.createCriteria(Lab.class);
  
              criteria.add(Restrictions.like("name", "%" + labSearch.getName() + "%"));
  
              if(labSearch.getState() != null){
                  criteria.createCriteria("states").add(Restrictions.eq("state", labSearch.getState())).addOrder(Order.asc("date")).list().get(0);
  
  
              return (List&lt;Lab&gt;) criteria.list();
  }
  </code></pre>
  
  <p>I don't nkow how to do this with Criteria, or if there is any other way to do</p>
  
  <p>thanks</p>