title: HQL sorting by sum of collection items' property value
body: <p>I hope someone can point me in the right direction with a Hibernate named query.</p>
  
  <p>I want to sort a result based on a property of a collection item. Think it is easiest by example:</p>
  
  <p>I have 2 classes: A and B</p>
  
  <p>A has a property "Bs" which is a set of B.</p>
  
  <p>B has a property "rank" which is an integer.</p>
  
  <p>I want to query for (all) As and sort them on the sum of B.rank in A.Bs.</p>
  
  <p>Something like:</p>
  
  <pre><code>select x from A as x order by sum(x.Bs.rank)
  </code></pre>
  
  <p>Hope this makes sense! The above query now throws an error "illegal attempt to dereference collection".</p>