JPQL:查询多列时,哪种对象包含结果列表? [英] JPQL: What kind of objects contains a result list when querying multiple columns?

查看:87
本文介绍了JPQL:查询多列时,哪种对象包含结果列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些简单的事情,例如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子句的结果类型 由...的结果类型定义 其中包含的 select_expressions 它.当多个 select_expressions 用在SELECT子句中 查询结果的类型 Object[],以及其中的元素 结果对应于 它们的规格顺序 SELECT子句,其类型为 每个的结果类型 select_expressions .

The result type of the SELECT clause is defined by the the result types of the select_expressions contained in it. When multiple select_expressions are used in the SELECT clause, the result of the query is of type Object[], and the elements in this result correspond in order to the order of their specification in the SELECT clause and in type to the result types of each of the select_expressions.

如果要进行强类型输入,可以在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列表返回一个或多个Java 实例.指定的类别不是 要求是实体或 映射到数据库.这 构造函数名称必须完整 合格的.

A constructor may be used in the SELECT list to return one or more Java instances. The specified class is not required to be an entity or to be mapped to the database. The constructor name must be fully qualified.

如果指定了实体类名称 在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:查询多列时,哪种对象包含结果列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆