简体   繁体   English

具有多个条件的QueryDSL Predicate SetPath.any

[英]QueryDSL Predicate SetPath.any with multiple conditions

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;
   ...
}

Now using QueryDSL, I would like to query 'All members enrolled in a program with programType = "FULLTIME" and programCost > $1000' 现在使用QueryDSL,我想查询'所有注册程序的成员,程序类型=“FULLTIME”和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 使用JPARepository

memberRepository.findAll(predicate);

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. 它返回al成员至少有一个类型为'FULLTIME'的程序或至少一个成本大于1000的程序。

Desired result : Return members if he has at least one program that is of type FULLTIME and cost > 1000. 期望的结果:如果他至少有一个类型为FULLTIME并且成本> 1000的程序,则返回成员。

Got some help here : https://groups.google.com/forum/#!topic/querydsl/hxdejLyqXos 在这里得到一些帮助: https//groups.google.com/forum/#!topic / querydsl / hxdejLyqXos

Basically the conditions on the program need to be in a separate subQuery (a JPASubquery instance) 基本上程序的条件需要在一个单独的子查询中( 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);

As mentionned by @Shahbour, this is not working anymore with QueryDsl 4.x+. 正如@Shahbour所提到的,这不再适用于QueryDsl 4.x +。

I had a similar case (except that my entities are bidirectionnal), and I've solved it with this : 我有一个类似的情况(除了我的实体是bidirectionnal),我用这个解决了它:

QProgram program = QProgram.program;
QProgram member  = QProgram.member;

Predicate predicate = JPAExpressions
    .selectOne()
    .from(program)
    .where(program.member.id.eq(member.id),
            program.programCost.gt(1000),
            program.programType.eq(ProgramType.FULLTIME))
    )
    .exists();

memberRepository.findAll(predicate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM