jpql按子查询顺序产生意外的AST节点异常 [英] jpql order by subquery produces unexpected AST node exception
本文介绍了jpql按子查询顺序产生意外的AST节点异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将一个工作(postgre)sql查询翻译成jpql,但是hibernate抛出一个
I translated a working (postgre)sql query to jpql, but hibernate throws a
org.hibernate.hql.ast.QuerySyntaxException :
意外的AST节点
异常
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;
...
}
这是我的jpql查询:
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!
推荐答案
JPQL不支持顺序中的子查询
。如果我正确理解您的查询,您可以尝试如下所示:
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
这篇关于jpql按子查询顺序产生意外的AST节点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文