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

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

问题描述

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

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:

DTO类看起来如下:

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;
    }
}

子类:

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();

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

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 :-/

推荐答案

这是不是一个答案。

JPA 2.0规范并没有,我可以看到,允许使用集合作为参数在构造函数表达式。第4.8节定义了一个这样的构造函数表达式:

The JPA 2.0 spec doesn't, as far as i can see, 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_pa​​th_expression 这个属性表达式指向某种类型的标量(例如 p.id ), scalar_expression 也是一个指向标量的表达式, aggregate_expression 是应用程序 sum 多值表达式转换为标量表达式,而 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. Assuming i've read the spec right.

所以,如果你的JPA提供者允许你在构造函数表达式中使用集合值参数,那是因为它超越了规格。这是非常好的,但不是你一定可以信赖的东西!

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天全站免登陆