title: Help with HQL query (group by, order by)
body: <p>I have problem writing HQL to display distinct applicationId with the latest application (newest createDate) for the following data in the table.</p>
  
  <pre><code>+---------------+-------------+----------+---------------------+
  | applicationId | firstName   | lastName | createDate          |
  +---------------+-------------+----------+---------------------+
  |             1 | Mark        | Johnson  | 2010-05-03 00:00:00 |
  |             3 | Henry       | Jordan   | 2010-05-03 00:00:00 |
  |             5 | Cindy Spahn | Wilson   | 2010-05-03 00:00:00 | 
  |             5 | Cindy Spahn | Wilson   | 2010-05-04 00:00:00 |
  |             5 | Cindy Spahn | Wilson   | 2010-05-05 00:00:00 |
  +---------------+-------------+----------+---------------------+
  5 rows in set (0.00 sec)
  </code></pre>
  
  <p>Below is the result that I'm looking for:</p>
  
  <pre><code>+---------------+-------------+----------+---------------------+
  | applicationId | firstName   | lastName | createDate          |
  +---------------+-------------+----------+---------------------+
  |             1 | Mark        | Johnson  | 2010-05-03 00:00:00 |
  |             3 | Henry       | Jordan   | 2010-05-03 00:00:00 |
  |             5 | Cindy Spahn | Wilson   | 2010-05-05 00:00:00 |
  +---------------+-------------+----------+---------------------+
  3 rows in set (0.00 sec)
  </code></pre>
  
  <p>Entities are as follow:</p>
  
  <pre><code>@Entity
  @Table(name = "application")
  public class Application {
      private long applicationId;
      private String firstName;
      private String lastName;
      private List&lt;ApplicationHistory&gt; applicationHistoryList;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public long getApplicationId() {
          return applicationId;
      }
  
      @OneToMany(mappedBy = "application", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      public List&lt;ApplicationHistory&gt; getApplicationHistoryList() {
          return applicationHistoryList;
      }
      // getter() and setter()
  }
  </code></pre>
  
  <p>and: </p>
  
  <pre><code>@Entity
  @Table(name = "applicationHistory")
  public class ApplicationHistory {
      private Application application;
      private final Timestamp createDate = new Timestamp(System.currentTimeMillis());
  
      @ManyToOne
      @JoinColumn(name = "applicationId", insertable = false, updatable = false)
      public Application getApplication() {
          return application;
      }
  
      @Id
      @Column(columnDefinition = "timestamp default current_timestamp")
      public Timestamp getCreateDate() {
          return createDate;
      }
  }
  </code></pre>