2011-04-05 67 views
2

我翻譯工作(postgre)SQL查詢來JPQL,但冬眠拋出一個由子查詢JPQL順序產生意外AST節點例外

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

任何人都可以解釋異常,爲什麼會發生以及如何更改查詢以避免它。謝謝!

回答

5

JPQL不支持order by中的子查詢。如果我沒有理解你的查詢,你可以嘗試這樣的事:

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

非常感謝,您的建議有輕微的調整工作很大:我不得不添加「p.id,p.user.id頁。索引「到group by子句 – maxmc 2011-04-05 12:54:53