Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

java - JPQL: Receiving a Collection in a Constructor Expression

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...