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.

Is is possible, in JPQL, to write something like

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).

Besides, the constructor must get only 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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top