QueryDSL join 및 subquery 및 case 문 사용 예시

case문 사용시 then 값은 enum값이 들지 않으니 String 으로 변환하여 사용한다. (querydsl 5.0)

(로직적으로 처리하자)

QTbEntity innerME = new QTbEntity("ime");
        return queryFactory
                .select(Projections.constructor(TbEntity.class, tbEntity.memberId, tbEntity.roleGroup, joinBEntity.commId, joinBEntity.enumA))
                .from(tbEntity)
                .leftJoin(joinAEntity).on(tbEntity.memberId.eq(joinAEntity.userId))
                .leftJoin(joinBEntity).on(joinAEntity.commId.eq(joinBEntity.commId))
                .where(tbEntity.roleGroup.ne(
                                JPAExpressions.select(new CaseBuilder()
                                                .when(joinBEntity.enumA.eq(EnumA.EQ)).then("EQ_NAME")
                                                .when(joinBEntity.enumA.eq(EnumA.DIFF)).then("DIFF_NAME")
                                                .otherwise(tbEntity.roleGroup)
                                        ).from(innerME)
                                        .leftJoin(joinAEntity).on(innerME.memberId.eq(joinAEntity.userId))
                                        .leftJoin(joinBEntity).on(joinAEntity.commId.eq(joinBEntity.commId))
                                        .where(innerME.memberId.eq(tbEntity.memberId))
                        )
                );

+ Recent posts