Mapping one-to-many relationship using QueryDSL SQL

java, jdbc, querydsl, sql

Solution

Querydsl provides result aggregation functionality for such cases http://www.querydsl.com/static/querydsl/3.1.1/reference/html/ch03s02.html#d0e1634

In this case it would be something like

query.from(audit)
     .innerJoin(participant).on(...)
     .transform(groupBy(audit.id).as(audit, list(participant)));

See these examples for other groupBy options https://github.com/mysema/querydsl/blob/master/querydsl-collections/src/test/java/com/mysema/query/collections/GroupByTest.java

Problem

Lets say I have this two bean entities: ``` public class Audit { private String code; private java.sql.Timestamp creationDate; private String creatorId; private java.sql.Timestamp deletionDate; private String description; private String id; private String name; private String notes; private Short status; private List<AuditParticipant> participants; } ``` and : ``` public class AuditParticipant { private String auditId; private String department; private String id; private String name; private String notes; private String role; private String surname; } ``` ... where `Audit` can have 1..n `Participants`, how can I use QueryDSL SQL to project the list of the participants into Audit bean (get all participants that belongs to audit)? The beans were generated using QueryDSL code generation. Thanks

Original source