5

在 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'
                                )));

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

谢谢

4

1 回答 1

7

如果您不需要任何子记录,则可以像这样通过转换表达查询而不进行分组(仅草图)

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

如果您需要聚合单个结果行,则 group by 转换很有用。

于 2013-10-02T12:38:25.483 回答