子查询的jpql顺序产生意外的AST节点异常

时间:2011-04-05 12:01:04

标签: hibernate playframework jpql

我将一个工作(postgre)sql查询翻译成jpql,但是hibernate抛出一个

  

org.hibernate.hql.ast.QuerySyntaxException:   意外的AST节点   例外

这些是我的核心模型类:

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

这是我的jpql查询:

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

任何人都可以解释异常,它为什么会发生以及如何更改查询以避免它。谢谢!

1 个答案:

答案 0 :(得分:6)

JPQL不支持order by中的子查询。如果我正确理解您的查询,您可以尝试这样的事情:

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