简体   繁体   English

QueryDSl 多级结果聚合

[英]QueryDSl multilevel result aggregation

QueryDsl provides a tool for aggregating results returned from the database as follows: QueryDsl 提供了一个用于聚合从数据库返回的结果的工具,如下所示:

http://www.querydsl.com/static/querydsl/latest/reference/html/ch03s02.html http://www.querydsl.com/static/querydsl/latest/reference/html/ch03s02.html

In the item: 3.2.4.在项目中:3.2.4。 Result aggregation结果聚合

But I don't know how to do a 3-level aggregation, and I didn't find any documentation of it, nor in the QueryDSL automated test class does this case appear, and it seems very strange that this is not possible, as this is a very common case.但是我不知道怎么做3级聚合,也没有找到相关文档,QueryDSL自动化测试类中也没有出现这种情况,这似乎很奇怪,这是不可能的,因为这是一个很常见的情况。

Here's the example of what I want to do:这是我想要做的示例:

DTO's classes : DTO的课程:

class DtoLevel1 {
    Long id;
    List<DtoLevel2> level2;
}

class DtoLevel2 {
    Long id;
    List<DtoLevel3> level3;
}

class DtoLevel3 {
    Long id;
}

Query:询问:

QDtoLevel1 dtoLevel1 = QDtoLevel1.dtoLevel1;
QDtoLevel1 dtoLevel2 = QDtoLevel1.dtoLevel1;
QDtoLevel1 dtoLevel3 = QDtoLevel1.dtoLevel1;

JPAQuery<DtoLevel1> query = new JPAQuery<DtoLevel1>();

// ... query.from, etc

query.transform(
    GroupBy.groupBy(dtoLevel1.id).as( // the first level is ok
            Projections.constructor(
                    DtoLevel1.class, dtoLevel1.id, 
                    GroupBy.list( // passing grouped items in the first level
                        Projections.constructor(
                            DtoLevel2.class, 
                            dtoLevel2.id, 
                            GroupBy.groupBy(dtoLevel2.id) // the problem is here
                                .list(
                                    Projections.constructor(DtoLevel3.class, dtoLevel3.id)
                                )
                        )
                    )
            )
    )
)

In the line where I highlighted my problem, is a conceptual code that is the way I believe it should work.在我强调我的问题的那一行中,是一个概念性代码,我认为它应该起作用。 Which is where I need to group the third level items belonging to the current second level, but I haven't found a way to do that, just for the first level.这是我需要对属于当前第二级的第三级项目进行分组的地方,但我还没有找到一种方法来做到这一点,仅适用于第一级。

Djefferson, I faced this problem too. Djefferson,我也遇到了这个问题。 Seems actually this problem has no solution yet.看来这个问题实际上还没有解决方案。 Multiple level support is a feature to do in QueryDsl roadmap, but is foreseen since 2015..多级支持是 QueryDsl 路线图中的一项功能,但自 2015 年起就预见到了。

I developed a lib to offer a approach to help this multiple level aggregation.我开发了一个库来提供一种方法来帮助这种多级聚合。 More details in: https://github.com/zisluiz/querydsl-object-binder .更多详情请见: https : //github.com/zisluiz/querydsl-object-binder

Below a example that how this lib can help a aggregation like that:下面是这个库如何帮助这样的聚合的示例:

JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);

query.from(_city)
        .join(_city.state, _state)
        .join(_state.country, _country);

List<Tuple> tupleResult = query.select(_city.id, _city.name,
        _state.id, _state.name,
        _country.id, _country.name).orderBy(_city.id.asc()).fetch();

List<City> cities = QueryDslBinder.to(tupleResult, City.class,
        new GroupByBinder()
            .key("id", _city.id).
            field("name", _city.name)
            .single("state", new GroupByBinder()
                    .key("id", _state.id)
                    .field("name", _state.name)
                    .single("country", new GroupByBinder()
                            .key("id", _country.id)
                            .field("name", _country.name)                   
                            .collection("states", new GroupByBinder()
                                    .key("id", _state.id)))));

It supports a variety of queries combinations in multiple levels, always using single object referente for each specified object.它支持多层次的多种查询组合,始终对每个指定对象使用单个对象引用。 Supports too subquery expressions path and many complex queries that are not possible to map into results with only QueryDsl api.支持太子查询表达式路径和许多仅使用 QueryDsl api 无法映射到结果的复杂查询。 Helped me a lot in company project.在公司项目中帮助了我很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM