在QueryDSL分组变换器中计数

Ned*_*edo 5 java database jdbc querydsl

在QueryDSL中使用groupby处理时,是否可以获得聚合结果的计数?我有以下查询:

query.from(catalog)
.innerJoin(qe).on(catalog.id.eq(qe.itemId))
.innerJoin(enterprise).on(enterprise.id.eq(qe.enterpriseId))

.leftJoin(catalogPerson).on(catalogPerson.catalogId.eq(catalog.id))

.where(catalog.deletionDate.isNull(), qe.enterpriseId.eq(org) )

.orderBy(catalog.creationDate.desc())

.limit(limit)
.offset(offset)

.transform(groupBy(catalog.id).as(Projections.constructor(Catalog.class,
                                catalog.id,
                                catalog.name,
                                catalog.code,
                                // //GET THE NUMBER OF CATALOG PERSONS'
                                )));
Run Code Online (Sandbox Code Playgroud)

我想得到属于某个目录的人数.

谢谢

Tim*_*per 6

如果您不需要任何子记录,那么通过这样的转换表示没有组的查询(仅绘制草图)

query.from(catalog)
.innerJoin(qe).on(catalog.id.eq(qe.itemId))
.innerJoin(enterprise).on(enterprise.id.eq(qe.enterpriseId))
.leftJoin(catalogPerson).on(catalogPerson.catalogId.eq(catalog.id))
.where(catalog.deletionDate.isNull(), qe.enterpriseId.eq(org))
.orderBy(catalog.creationDate.desc())
.limit(limit)
.offset(offset)
.groupBy(catalog.id)
.list(Projections.constructor(Catalog.class,
                            catalog.id,
                            catalog.name,
                            catalog.code,
                            catalogPerson.count()));
Run Code Online (Sandbox Code Playgroud)

如果需要聚合单个结果行,则按转换组很有用.

  • 谢谢蒂莫.只是一个小建议.如果有人可以为一些现实生活中的例子详细说明"QueryDSL配方",那将是很好的,这在文档中没有提到,就像这个. (3认同)