简体   繁体   English

在JPQL中,是否可以使用列表作为参数编写“选择新的”?

[英]In JPQL, is it possible to write a “select new” with a list as parameter?

I wonder if it is possible to write a "select new" query with a list as a parameter. 我想知道是否可以用列表作为参数编写“选择新”查询。

For example, in one hand, I have a table "father" and a table "children". 例如,一方面,我有一张桌子“父亲”和一张桌子“孩子”。 A children has only one father and a father has multiple children. 一个孩子只有一个父亲,一个父亲有多个孩子。 In the other hand, I have an object "FatherDto" which the constructor need a "Father" object and a list of children. 另一方面,我有一个对象“ FatherDto”,构造函数需要一个“父亲”对象和一个子代列表。

Is is possible, in JPQL, to write something like 在JPQL中可以写类似

SELECT new FatherDto(fat, childrenList) 
FROM fat, (select new Child(ch) from children ch where ...) as childrenList from children child
WHERE ...

The objective is to get, using only one query, a list of father with a list of children in it. 目的是仅使用一个查询就可以获取一个父亲列表和一个孩子列表。

No, you cannot do that, because "subqueries may be used in the WHERE or HAVING clause" (from spec). 不,您不能这样做,因为“子查询可以在WHERE或HAVING子句中使用”(来自规范)。

Besides, the constructor must get only single_values_path_expression. 此外,构造函数必须仅获得single_values_path_expression。 Excerpt from the specification: 规范摘录:

select_clause ::= SELECT [DISTINCT] select_item {, select_item}*
select_item ::= select_expression [ [AS] result_variable]
select_expression ::=
  single_valued_path_expression |
  scalar_expression |
  aggregate_expression |
  identification_variable |
  OBJECT(identification_variable) |
  constructor_expression
constructor_expression ::=
  NEW constructor_name ( constructor_item {, constructor_item}* )
constructor_item ::=
  single_valued_path_expression |
  scalar_expression |
  aggregate_expression |
  identification_variable
aggregate_expression ::=
  { AVG | MAX | MIN | SUM } ([DISTINCT] state_valued_path_expression) |
  COUNT ([DISTINCT] identification_variable | state_valued_path_expression |
      single_valued_object_path_expression) |
  function_invocation

and I am not sure whether in new FatherDto(fat, childrenList) childrenList is considered a path expression. 并且我不确定在new FatherDto(fat, childrenList) childrenList中是否将其视为路径表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM