title: jpql order by subquery produces unexpected AST node exception
body: <p>I translated a working (postgre)sql query to jpql, but hibernate throws a</p>
  
  <blockquote>
    <p>org.hibernate.hql.ast.QuerySyntaxException:
    unexpected AST node
    exception</p>
  </blockquote>
  
  <p>These are my core model classes:</p>
  
  <pre><code>@Entity
  public class Piece {
      @Id
      @GeneratedValue
      public Long id;
  
      @ManyToOne
      public AUser user;
      public long index;
  ...
  }
  
  @Entity
  public class Vote {
      @Id
      @GeneratedValue
      public Long id;
  
      @ManyToOne
      public AUser receiver;
  ...
  }
  
  @Entity
  public class AUser {
      @Id
      @GeneratedValue
      public Long id;
  
      @OneToMany(mappedBy="receiver", cascade=CascadeType.ALL)
      public List&lt;Vote&gt; receivedVotes;
  ...
  }
  </code></pre>
  
  <p>Here is my jpql query:</p>
  
  <pre><code>String query = "select p from Piece p order by (select count(v.receiver) from Vote v where v.receiver.id=p.user.id) desc, p.index";
  </code></pre>
  
  <p>Can anyone explain the exception, why it happens and how to change the query to avoid it. Thanks!</p>