title: HQL query for entity with max value
body: <p>I have a Hibernate entity that looks like this (accessors ommitted for brevity):</p>
  
  <pre><code>@Entity
  @Table(name="FeatureList_Version")
  @SecondaryTable(name="FeatureList",
      pkJoinColumns=@PrimaryKeyJoinColumn(name="FeatureList_Key"))
  public class FeatureList implements Serializable {
  
      @Id
      @Column(name="FeatureList_Version_Key")
      private String key;
  
      @Column(name="Name",table="FeatureList")
      private String name;
  
      @Column(name="VERSION")
      private Integer version;
  
  }
  </code></pre>
  
  <p>I want to craft an HQL query that retrieves the most up to date version of a FeatureList. The following query sort of works:</p>
  
  <pre><code>Select f.name, max(f.version) from FeatureList f group by f.name
  </code></pre>
  
  <p>The trouble is that won't populate the key field, which I need to contain the key of the record with the highest version number for the given FeatureList. If I add f.key in the select it won't work because it's not in the group by or an aggregate and if I put it in the group by the whole thing stops working and it just gives me every version as a separate entity.</p>
  
  <p>So, can anybody help?</p>