jpa QueryDSL筛选集合中的所有元素

izkcnapc  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(125)
@Getter
@Setter
@ToString

@ApiModelProperty("contains ignore case")
private String name;
@ApiModelProperty("equals")
private String department;
@ApiModelProperty("in equals")
private List<String> position;
@ApiModelProperty("greater or equals")
private Integer salaryFrom;
@ApiModelProperty("lower or equals")
private Integer salaryTo;
@ApiModelProperty("contains ignore case")
private String email;
@ApiModelProperty("at least one with priority higher")
private Integer oneHigher;
@ApiModelProperty("all priorities higher")
private Integer allHigher;

BooleanBuilder conditions = new BooleanBuilder();
ofNullable(salaryFrom).map(employee.salary::goe).ifPresent(conditions::and);
ofNullable(salaryTo).map(employee.salary::loe).ifPresent(conditions::and);
ofNullable(email).map(employee.email::containsIgnoreCase).ifPresent(conditions::and);
ofNullable(oneHigher).map(employee.tasks.any().priority::goe).ifPresent(conditions::and);                              
return conditions;

我有一个Employee实体,该实体具有Task集,我希望能够仅查询所有任务的优先级都高于某个优先级的员工。我可以使用any()来实现这一点,但当至少一个任务的优先级高于某个值时,我无法找到针对所有任务执行此操作的方法,因为我无法在employee.tasks中迭代任务

cpjpxq1n

cpjpxq1n1#

我不知道如何用QueryDSL对此建模,但通常在HQL中,这可以通过使用exists子查询来建模,例如:

from Employee e
where not exists (
  select 1
  from e.tasks t
  where t.priority < :priority
)

另一种表述方式是使用量词

from Employee e
where :priority > all (
  select t.priority
  from e.tasks t
)

相关问题