JPQL:在构造函数表达式中接收集合 [英] JPQL: Receiving a Collection in a Constructor Expression

查看:25
本文介绍了JPQL:在构造函数表达式中接收集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JPQL 并希望在构造函数表达式中接收一些普通参数和集合以直接创建 DTO 对象.但是如果 Collection 是空的,我总是报错,因为他没有找到正确的构造函数:

DTO-Class 如下所示:

公共类 DTO {私人长ID;私人字符串名称;私人收藏<儿童>孩子们;公共 DTO(长 ID、字符串名称、集合<子项>子项){this.id = id;this.name = 名称;this.children= 儿童;}}

儿童班:

public class Child {私人字符串名称;私人整数年龄;}

现在构造函数表达式如下所示:

return (List) getEm().createQuery("SELECT DISTINCT NEW de.DTO(p.id, p.name, p.childs)从父 p").getResultList();

当前的问题是,如果集合 p.childs 为空,则表示它没有找到正确的构造函数,它需要 (long, String, Child) 而不是 (long, String, Collection).

您是否有任何解决方案,或者根本不可能在构造函数表达式中使用集合?

哦,还有一件事:如果我轻松创建两个构造函数(...,Collection childs AND ...,Child childs)我没有得到任何结果,但也没有错误......在我看来并不真正令人满意:-/

解决方案

JPA 规范(至少 2.0、2.1 和 2.2 版)不允许在构造函数表达式中使用集合作为参数.4.8 节定义了这样的构造函数表达式:

constructor_expression ::=新的constructor_name(constructor_item {,constructor_item}*)构造函数 ::=single_valued_pa​​th_expression |标量表达式 |聚合表达式 |识别变量

single_valued_pa​​th_expression 听起来像 - 指向某种标量(例如 p.id)的属性表达式,scalar_expression 也是一个指向标量的表达式,aggregate_expression 是一个类似于 sum 的函数的应用,它将多值表达式简化为一个标量表达式,一个identification_variable 是对您正在查询的类型的引用.没有一个是收藏价值的.

因此,如果您的 JPA 提供程序允许您在构造函数表达式中使用集合值参数,那是因为它超出了规范.这是非常好的,但不是您必须依赖的东西!

I'm using JPQL and want to receive some normal parameters and a collection in a Constructor Expression to directly create the DTO-objects. But if the Collection is empty, I always get a error because he doesnt find the right constructor:

The DTO-Class looks the following:

public class DTO {
    private long id;
    private String name;
    private Collection<Child> children;

    public DTO (long id, String name, Collection<Child> children){
    this.id = id;
    this.name = name;
    this.children= children;
    }
}

The Child-Class:

public class Child {
    private String name;
    private int age;
}

And now the Constructor Expression looks the following:

return (List<DTO>) getEm().createQuery("SELECT DISTINCT NEW de.DTO(p.id, p.name, p.childs) 
                                          FROM Parent p").getResultList();

The current problem is, that in case the Collection p.childs is empty, it says that it doesnt find the right constructor, it needs (long, String, Child) instead of (long, String, Collection).

Do you have any kind of solution or is it simply not possible to use a Collection in Constructor Expression?

Oh and one more thing: If I easily create two constructors (..., Collection childs AND ..., Child childs) I get no results, but no error too... in my eyes not really satisfiyng :-/

解决方案

The JPA spec (Version 2.0, 2.1 and 2.2 at least) doesn't allow the use of collections as parameters in constructor expressions. Section 4.8 defines a constructor expression like this:

constructor_expression ::=
        NEW constructor_name ( constructor_item {, constructor_item}* )
constructor_item ::=
        single_valued_path_expression |
        scalar_expression |
        aggregate_expression |
        identification_variable

A single_valued_path_expression is what it sounds like - a property expression that points to a scalar of some sort (such as p.id), a scalar_expression is also an expression that points to scalar, an aggregate_expression is the application of a function like sum which reduces a multi-valued expression to a scalar one, and an identification_variable is a reference to the type you're querying over. None of which can be collection-valued.

So, if your JPA provider lets you use collection-valued parameter in a constructor expression, it's because it's going above and beyond the spec. Which is very nice of it, but not something you can necessarily rely on!

这篇关于JPQL:在构造函数表达式中接收集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆