有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在JPQL中,是否可以编写一个以列表为参数的“select new”?

我想知道是否有可能编写一个以列表为参数的“select new”查询

例如,一方面,我有一张桌子“父亲”和一张桌子“孩子”。一个孩子只有一个父亲,而一个父亲有多个孩子。另一方面,我有一个对象“FatherDto”,构造函数需要一个“Father”对象和一系列子对象

在JPQL中,可以编写如下内容

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

目标是只使用一个查询来获取一个父亲列表,其中包含一个孩子列表


共 (1) 个答案

  1. # 1 楼答案

    不,您不能这样做,因为“子查询可以在WHERE或HAVING子句中使用”(来自规范)

    此外,构造函数必须只获取单个_值_路径_表达式。本规范摘录:

    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
    

    我不确定in new FatherDto(fat, childrenList)childrenList是否被视为路径表达式