slider
slider

Reputation: 431

How with QueryDSL get the map of ids of one entity and the numbers of associated entities

How to get the map of ids of one entity (key) and the numbers of associated entities (value). Without transfer entities. Something like this:

from(qE1)
   .join(qE2).on(qE2.qE1_id.eq(qE1.id))
   .where(qE1_exp)
   .transform(GroupBy.groupBy(qE2.id).as(qE1.count())) // ?

QueryDSL-4.2.1.

Upvotes: 0

Views: 1057

Answers (1)

Reza
Reza

Reputation: 54

I am not an expert with QueryDSL but I believe something like this or similar using JPAQueryFactory could do it.

Map<Long, Long> entityMap = new JPAQueryFactory(entityManager)
.select(qE2.qE1.id, qE2.count())
.from(qE2)
.join(qE2.qE1)
.where(qE1_exp)
.groupBy(qE2.qE1.id)
.fetch()
.stream()
.collect(Collectors.toMap(
    tuple -> tuple.get(qE2.qE1.id),
    tuple -> tuple.get(qE2.count())
));

Upvotes: 2

Related Questions