我想知道是否可以用列表作为参数编写“选择新”查询。
例如,一方面,我有一张桌子“父亲”和一张桌子“孩子”。一个孩子只有一个父亲,一个父亲有多个孩子。另一方面,我有一个对象“ FatherDto”,构造函数需要一个“父亲”对象和一个子代列表。
在JPQL中可以写类似
SELECT new FatherDto(fat, childrenList)
FROM fat, (select new Child(ch) from children ch where ...) as childrenList from children child
WHERE ...
Run Code Online (Sandbox Code Playgroud)
目的是仅使用一个查询就可以获取一个父亲列表和一个孩子列表。
不,你不能这样做,因为“子查询可以在 WHERE 或 HAVING 子句中使用”(来自规范)。
此外,构造函数必须仅获取 single_values_path_expression。规范摘录:
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
Run Code Online (Sandbox Code Playgroud)
我不确定 in new FatherDto(fat, childrenList)ChildrenList 是否被视为路径表达式。
| 归档时间: |
|
| 查看次数: |
747 次 |
| 最近记录: |