Search code examples
springspring-dataspring-data-jpaquerydsl

Querydsl join condition


I'm using querydsl with Spring JPA. I want to find entities that has a subitem with two conditions. This code works with a single condition (i.e. any.selected.isTrue()). However they do not work together. How can such a query be expressed using querydsl?

QSub any = exp.sub.any();
    builder.and(
        any.selected.isTrue().and(any.sub.person.id.eq(user.getId()))));

where builder is a boolean builder.


Solution

  • I found that (as suggested by other posts) creating a subquery solved the problem:

    JPQLQuery<Tuple> where = JPAExpressions.select().from(QSub.sub).where(QSub.sub.id.eq(QMain.main.id), QSub.sub.selected.isTrue(), QSub.sub.userId.eq(user.getId()));
    

    "Where" can then be added to the boolean builder as a condition.