【发布时间】:2015-05-07 11:14:49
【问题描述】:
作为 JSR-338 的一部分,出现了使用构造函数表达式的特性。
问题是在使用构造函数表达式时如何使用order by。
以 JPQL 为例:
select com.acme.AggregatedInfo(
c,
(select status from Account where ssn = c.ssn)
)
from Customer c
where m.id in (:customerIdList)
还有 AggregatedInfo 类
class AggregatedInfo {
private final Customer customer;
private final String status;
public AggregatedInfo(Customer customer, String status) {...}
// boilerplate javacode....
}
我在这样的 DAO 中使用它:
public List<AggregatedInfo> getAggregatedResult(final List<Long> customerIdList)
return em.createQuery(hql, AggregatedInfo.class)
.setParameters("customerIdList", customerIdList)
.getResultList();
}
如果我想按状态订购 - 如何通过 JPQL 完成?
我尝试了以下方法但没有成功:
select com.acme.AggregatedInfo(
c,
(select status from Account where ssn = c.ssn)
) as a
from Customers c
where m.id in (:customerIdList)
order by c.status
但这不起作用。
可行吗? 请解释一下。
【问题讨论】:
-
您使用的是 JPA 特定的方法和类还是 Hibernate?
-
我正在使用 JPA api
-
你能告诉我你的 DAO 课程吗?
-
我已将 dao 代码添加到问题中。
标签: java hibernate jpql jpa-2.1