QueryDSL Predicate SetPath.any with multiple conditions

I have a simple object with a one-to-many relationship.

@Entity // and other @ stuff public class Member { @Id private Long id; private String name; private List<Program> programs; ... } @Entity public class Program { @Id private Long id; private Long programName; private ProgramType programType; private Long programCost; ... } 

Now, using QueryDSL, I would like to query "All participants included in the program with programType =" FULLTIME "and programCost> $ 1000 '

I used the following predicate

 Predicate predicate = QMember.member.programs.any() .programType.eq(ProgramType.FULLTIME) .and(QMember.member.programs.any().programCost.gt(1000)); 

with JPARepository

 memberRepository.findAll(predicate); 

Now the problem is that these two queries are independent. It returns al members with at least one program of type FULLTIME or at least one program worth more than 1000.

Desired result: return elements if it has at least one program with type FULLTIME and cost> 1000.

+6
source share
1 answer

Get some help here: https://groups.google.com/forum/#!topic/querydsl/hxdejLyqXos

Basically, program conditions should be in a separate subcategory (a JPASubquery )

 QProgram program = QProgram.program JPASubQuery subQuery = new JPASubQuery(); subQuery.from(program) .where(program.programType.eq(ProgramType.FULLTIME), program.programCost.gt(1000)); Predicate predicate = QMember.member.name.eq("John") .and(subQuery.exists()); memberRepository.findAll(predicate); 
+8
source

All Articles