QueryDsl with GroupBy and Paging

472 views
Skip to first unread message

Bob Wilson

unread,
Jan 7, 2016, 11:44:57 AM1/7/16
to Querydsl
Is there a way to implement a groupBy queryDsl statement that includes paging? It appears that using groupBy and transform you end up with a map and I have not seen any documentation around implementing paging with grouping. 

Map<String, PcServiceConnection> results = getQuerydsl().createQuery(qPcServiceConnection).
                where(qPcServiceConnection.targetOrgId.in(targetOrgIds)).
                transform(groupBy(qPcServiceConnection.patientEpisodeDocKey).as(Projections.bean(PcServiceConnection.class, qPcServiceConnection.patientEpisodeDocKey,
                        qPcServiceConnection.patientReferralDocKey, qPcServiceConnection.pcServiceConnectionId)));
        */

timowest

unread,
Jan 7, 2016, 2:00:29 PM1/7/16
to Querydsl
The paging (limit/offset) applies to the query and not result aggregation. Paging at the result aggregation is not supported.

Could you explain your requirement for that?

Bob Wilson

unread,
Jan 7, 2016, 5:42:24 PM1/7/16
to Querydsl
I have a query like this that does a groupBy for aggregation and creates a list of objects. I have not been able to find the syntax - or if it is possible - to integrate paging and sorting into a query that does grouping:.
      
       Collection customers = getQuerydsl().createQuery(qCustomer).
       where(qCustomer.customerId.in(targetIds)).
       transform(groupBy(qCustomer.ssn).
           as(Projections.bean(Customer.class, qCustomer.Id,
                 qCustomer.ssn, qCustomer.name))).
       values(); 

Alan C.

unread,
Oct 15, 2022, 11:16:09 AM10/15/22
to Querydsl
Hello Bob,

Hope this fits you well, but for us with latest version, we tried this approach. The only problem we have, is that we want to sort by a counter on a property. It's not working, but for other values it does.

final var querydsl = new Querydsl(entityManager, (new PathBuilderFactory())
.create(ConfigElement.class));

JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
JPAQuery<ConfigElement> configElementJPAQuery = queryFactory
.selectDistinct(configElement).from(configElement)
.leftJoin(configElement.milestones, milestone)
.leftJoin(configElement.reviews, review)
.where(configElementsFilter)
.orderBy(orderSpecifiers);

final List<?> result = querydsl.applyPagination(pageable, configElementJPAQuery).fetchAll().fetch();


Greets,

Reply all
Reply to author
Forward
0 new messages