问题描述
我有一个简单的实体,有一对多关系
I have a simple entity with 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;
...
}
现在使用QueryDSL,我想查询
'所有注册programType =FULLTIME和programCost> $ 1000'
Now using QueryDSL, I would like to query 'All members enrolled in a 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
with JPARepository
memberRepository.findAll(predicate);
现在的问题是这两个查询是独立的。它返回al成员至少有一个'FULLTIME'类型的程序或至少一个成本大于1000的程序。
Now the problem is that the two queries are independent. It returns al members with at least one program of type 'FULLTIME' or at least one program of cost greater than 1000.
所需结果:如果他至少有回报成员一个类型为FULLTIME且成本> 1000的程序。
Desired result : Return members if he has at least one program that is of type FULLTIME and cost > 1000.
推荐答案
在这里得到一些帮助:
Got some help here : https://groups.google.com/forum/#!topic/querydsl/hxdejLyqXos
基本上,程序的条件需要在一个单独的子查询中(a 实例)
Basically the conditions on the program need to be in a separate subQuery (a JPASubquery instance)
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);
这篇关于具有多个条件的QueryDSL Predicate SetPath.any的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!