【发布时间】:2011-05-27 17:20:35
【问题描述】:
背景
我有两个使用 Hibernate 持久化的对象。产品和投票。
Product对象包含一个投票列表,比如
public class Product {
@javax.persistence.Id
@javax.persistence.GeneratedValue
public Long id;
String name;
@OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
List<Vote> votes;
}
Vote 对象只保存分数和产品的双向链接。
public class Vote {
@javax.persistence.Id
@javax.persistence.GeneratedValue
public Long id;
Short score;
@ManyToOne
Product product;
}
这很好用,在我的数据库中我可以看到预期的产品和投票。 现在,我想编写一个 JPQL 查询以按最高平均票数排序产品。
我使用纯 SQL 实现了这一点,如下所示
select p.name
from product p
order by (select avg(score) from vote v where p.id = v.product_id) desc;
问题
我似乎无法在我的 JPQL 查询中使用它。我只是收到一条错误消息说"unexpected AST node".
我使用的 JPQL 是
select p from Product p
order by (select avg(score) from Vote v where p.id = v.product) desc
JPQL 不支持内部选择语句吗?
【问题讨论】: