jpql order by subquery produces unexpected AST node exception

陌路散爱 提交于 2019-12-20 02:08:26

问题


I translated a working (postgre)sql query to jpql, but hibernate throws a

org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node exception

These are my core model classes:

@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<Vote> receivedVotes;
...
}

Here is my jpql query:

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";

Can anyone explain the exception, why it happens and how to change the query to avoid it. Thanks!


回答1:


JPQL doesn't support subqueries in order by. If I correctly understand your query, you can try something like this:

select p 
from Piece p left join p.user.receivedVotes rv
group by p
order by count(rv) desc, p.index


来源:https://stackoverflow.com/questions/5551459/jpql-order-by-subquery-produces-unexpected-ast-node-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!