Querry and Projections

68 views
Skip to first unread message

Cristian Colombo

unread,
Sep 7, 2017, 6:55:21 AM9/7/17
to Querydsl
Hi,
in my project, I have this situation:

@Data
@AllArgumentsConstructor
EntityADTO{
   
Long id;
   
String name;
   
Set<EntityB>;  
}

@Data
@AllArgumentsConstructor
EntityBDTO{
   
Long id;
   
String name;
}

All class are annotated with Lombok annotations.

What is the correct dsl query, to retrieve only one dto EntityADTO with all populated set of EntityBDTO?
I
try the next query, but the results is List of Tuple of projections based by column extracted in defaultEntityAExpression() method and the Set<EntityB> is populated with only one entityB.

sqlQueryFactory.select(defaultEntityAExpression())
.from(
entityA)
.join(
entityB).on(entityB.fk.eq(entityA.id))

public static Expression<EntityADTO> defaultEntityAExpression() {
//@formatter:off
return Projections.constructor(EntityADTO.class,
entityA.id,
entityA.name,
Projections.list(defaultEntityBexpression()));
//@formatter:on
}

public static Expression<EntityBDTO> defaultEntityBexpression() {
//@formatter:off
return Projections.constructor(
EntityBDTO.class,
entityB.id,
entityB.name);

//@formatter:on
}






I'm using the querydsl sql module (no JPA, no JDO etc.). I would like to get the same results that I would get by using hibernate 
Thanks in advance,
Cristian

Liam Coughlin

unread,
Mar 6, 2018, 12:45:48 PM3/6/18
to Querydsl
Hi -- To my knowledge you can't exactly, however you can do something like:


Map<EntityADTO, List<EntityBDTO>> result = sqlQueryFactory.select(defaultEntityAExpression())
       
.from(entityA)
       
.join(entityB).on(entityB.fk.eq(entityA.id))
       
.transform(GroupBy.groupBy(entityA)).as(list(entityB));


I know that's not exactly what you're looking for but if you poke at it, you can probably come up with a ResultTransformer that does what you want.  If you do, please post it back to the group, you're probably not the only one interested.


Reply all
Reply to author
Forward
0 new messages