Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JPQL, is it possible to write a "select new" with a list as parameter?

Tags:

java

sql

jpa

jpql

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.

like image 457
Beleg Avatar asked Oct 01 '22 01:10

Beleg


1 Answers

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.

like image 115
V G Avatar answered Oct 30 '22 09:10

V G