问题描述
我正在尝试做一些简单的事情,例如PHP&合作:选择COUNT(x)作为numItems,平均AVG(y),... FROM Z
I'm trying to do something which is easy as pie in PHP & Co:SELECT COUNT(x) as numItems, AVG(y) as average, ... FROM Z
在PHP中,我会得到一个简单的数组,例如[{numItems:0,average:0}],可以这样使用:
In PHP I would get a simple array like [{ numItems: 0, average: 0 }] which I could use like this:
echo "Number of Items: " . $result[0]['numItems'];
通常,在JPQL中,您仅查询单个对象或单个列并获取列表类型,例如List<SomeEntity>
或List<Long>
.但是,查询多列时会得到什么?
Usually in JPQL you only query single objects or single columns and get Lists types, for example List<SomeEntity>
or List<Long>
. But what do you get, when querying multiple columns?
推荐答案
您将获得Object[]
(或List<Object[]>
).在JPA 1.0规范的 4.8.1 SELECT子句的结果类型部分中:
You get an Object[]
(or a List<Object[]>
). From the section 4.8.1 Result Type of the SELECT Clause of the JPA 1.0 specification:
如果要进行强类型输入,可以在SELECT子句中使用构造函数表达式.从 4.8.2 SELECT子句中的构造函数表达式部分开始:
If you want strong typing, you can use a constructor expression in the SELECT clause. From the section 4.8.2 Constructor Expressions in the SELECT Clause:
如果指定了实体类名称 在SELECT NEW子句中 结果实体实例位于 新状态.
If an entity class name is specified in the SELECT NEW clause, the resulting entity instances are in the new state.
SELECT NEW com.acme.example.CustomerDetails(c.id, c.status, o.count)
FROM Customer c JOIN c.orders o
WHERE o.count > 100
这篇关于JPQL:查询多列时,哪种对象包含结果列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!