I translated a working (postgre)sql query to jpql, but hibernate throws a
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node exception
JPQL doesn't support subqueries in order by. If I correctly understand your query, you can try something like this:
order by
select p from Piece p left join p.user.receivedVotes rv group by p order by count(rv) desc, p.index