java QueryDSL 中的多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12427665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:50:54  来源:igfitidea点击:

Multiple columns in QueryDSL

javadatabasequerydsl

提问by Gon?alo Cardoso

I'm trying to get a list of multiple columns from my table using QueryDSL, and automatically fill my DB object, like this example in an older manual:

我正在尝试使用 QueryDSL 从我的表中获取多列的列表,并自动填充我的 DB 对象,就像旧手册中的这个例子:

List<CatDTO> catDTOs = query.from(cat)
    .list(EConstructor.create(CatDTO.class, cat.id, cat.name));

The problem is that it looks like the EConstructor class was removed in version 2.2.0, and all the examples I find now are like this:

问题是2.2.0版本好像去掉了EConstructor类,我现在找到的所有例子都是这样的:

List<Object[]> rows = query.from(cat)
    .list(cat.id, cat.name);

Which forces me to manually cast all the objects into my CatDTO class.

这迫使我手动将所有对象转换为 CatDTO 类。

Is there any alternative to this? Any EConstructor alternative?

有什么替代方法吗?任何 EConstructor 替代方案?

采纳答案by Timo Westk?mper

EConstructor has been replaced with ConstructorExpression in Querydsl 2.0. So your example would become

EConstructor 已被 Querydsl 2.0 中的 ConstructorExpression 取代。所以你的例子会变成

List<CatDTO> catDTOs = query.from(cat)
    .list(ConstructorExpression.create(CatDTO.class, cat.id, cat.name));

You can also annotate the CatDTO constructor and query like this

您还可以像这样注释 CatDTO 构造函数和查询

List<CatDTO> catDTOs = query.from(cat)
    .list(new QCatDTO(cat.id, cat.name));

Alternatively you can use the QTuple projection which provides a more generic access option

或者,您可以使用 QTuple 投影,它提供了更通用的访问选项

List<Tuple> rows = query.from(cat)
    .list(new QTuple(cat.id, cat.name));

The actual values can be accessed via their path like this

可以像这样通过它们的路径访问实际值

tuple.get(cat.id)

and

tuple.get(cat.name)

Tuple projection will probably be used in Querydsl 3.0 for multiple columns projections instead of Object arrays.

元组投影可能会在 Querydsl 3.0 中用于多列投影而不是对象数组。

回答by Rafael Zeffa

using queryDSL 4 and Java 8 stream:

使用 queryDSL 4 和 Java 8 流:

List<CatDTO> cats = new JPAQueryFactory(entityManager)
        .select(cat.id, cat.name)
        .from(cat)
        .fetch()
        .stream()
        .map(c -> new CatDTO(c.get(cat.id), c.get(cat.name)))
        .collect(Collectors.toList());

回答by Daniel Higueras

Another alternative is to use the class Projections. It will construct the object using the fields you pass as parameters, like the EConstructor. Example:

另一种选择是使用 class Projections。它将使用您作为参数传递的字段来构造对象,例如EConstructor. 例子:

List<CatDTO> catDTOs = query.from(cat)
    .list(Projections.bean(CatDTO.class, cat.id, cat.name));

Reference: http://www.querydsl.com/static/querydsl/4.0.5/apidocs/com/querydsl/core/types/Projections.html

参考:http: //www.querydsl.com/static/querydsl/4.0.5/apidocs/com/querydsl/core/types/Projections.html