简体   繁体   English

按数量查询的变压器组

[英]querydsl transformer group by count

I am stuck trying to get a query (QueryDSL) to work that gives me a count of distinct categories. 我一直在努力让查询(QueryDSL)正常工作,这使我有了许多不同的类别。 For example, what I am trying to achieve: 例如,我要实现的目标:

categoryA -> 10 entries
categoryB -> 20 entries

This is what i have so far: 这是我到目前为止所拥有的:

query().from(application)
            .transform(groupBy(application.category).as(list(application)));

However, this gives me for each category a list of all whole entries, I just want to get a count of this. 但是,这为我提供了每个类别所有全部条目的列表,我只想对此进行计数。

I tried messing around with count() but no luck. 我试图弄乱count()但是没有运气。

Anybody know how to do this? 有人知道该怎么做吗?

Note that as of Querydsl 4.x , the accepted answer by Timo Westkämper is no longer valid. 请注意,从Querydsl 4.x开始TimoWestkämper接受的答案不再有效。 Breaking changes in Querydsl 4.0 have removed the query.list() method . Querydsl 4.0中的重大更改已删除了query.list()方法

A solution working with Querydsl 4.0+ would now be: 现在,使用Querydsl 4.0+的解决方案是:

query.from(application)
     .groupBy(application.category)
     .select(application.category, application.category.count())
     .fetch();

transform combined groupBy is meant to construct tree structures out of flat result sets. 转换组合groupBy的目的是从平面结果集中构造树结构。 What you need can be easier expressed as 您需要的内容可以更容易地表示为

query.from(application)
     .groupBy(application.category)
     .list(application.category, application.category.count())

If using 4.1.4, you may need to use a JPAQueryFactory instead of JPAQuery 如果使用4.1.4,则可能需要使用JPAQueryFactory而不是JPAQuery

If using JPAQuery, there is no select() or fetch() on the return from groupBy. 如果使用JPAQuery,则groupBy的返回值上没有select()或fetch()。

JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

queryFactory
     .select(application.category, application.category.count())
     .from(application)
     .groupBy(application.category)
     .fetch();

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

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