作为JSR-338的一部分,出现了使用Constructor Expressions的功能。
问题是我在使用构造函数表达式时如何使用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
但这不起作用。
可行吗? 请解释一下。
答案 0 :(得分:1)
尝试以下方法。它在Hibernate中的类似实现对我有用。它也应该对你有用
public List<AggregatedInfo> getAggregatedResult(final List<Long> customerIdList)
return em.createNativeQuery(hql, AggregatedInfo.class)
.setParameters("customerIdList", customerIdList)
.getResultList();
}
将entityManager.createQuery()
替换为entityManager.createNativeQuery()