【问题标题】:Boolean Expression for two conditions of an element of a list列表元素的两个条件的布尔表达式
【发布时间】:2020-01-02 00:41:06
【问题描述】:

我在这里使用 QueryDSL 来构建谓词,问题是这个谓词需要两个 AND 条件,我被困在这里,因为我不知道如何应用第二个条件。

这是场景:

我有一个设备,这个设备属于一个实验室,这个实验室有一个团队。该实验室团队的一些成员可以注册新设备,而有些则不能,这是由布尔属性“registerEquipment”定义的。所以我的谓词必须有这些条件:

*用户必须是实验室团队的成员并且将属性“registerEquipment”设置为 true 才能查看此设备。我尝试了以下代码:

BooleanExpression registerEquipmentAndTeamMember = QEquipment.equipment.laboratory.team.any().registerEquipment.and(QEquipment.equipment.laboratory.team.any().person.id.eq("AQ88"));

但不起作用,因为它首先执行 registerEquipment 的条件,然后是 teamMember。我想知道如何同时为团队成员执行这两个条件。

【问题讨论】:

  • 你希望通过让它们同时执行来发生什么/让其中的一部分先执行有什么问题?
  • @MarcSlothEastman 通过同时执行这两个条件,它将正确地向我返回 registerEquipment 为 true 的人和实验室团队的成员。如果它单独执行,它将返回来自实验室团队的人员和未注册设备的人员。这就是问题...
  • 为什么你认为这是问题所在?鉴于这两个查询都只是一个“get”/SELECT 类型的查询,它们不会相互影响。我建议单独尝试查询并在此处发布结果
  • @MarcSlothEastman 因为 QueryDSL 生成的查询中存在两个而不是一个。例如,QueryDSL 不是检查当前用户是否是实验室团队的成员和 registerEquipment=true,而是检查是否有人具有 registerEquipment=true,然后检查当前用户是否是实验室团队的成员。如果当前用户是实验室团队的一员并且 registerEquipment=true,那么正确的情况是。
  • @MarcSlothEastman 就像这样存在(从设备.laboratory.team 中选择 1 作为设备_laboratory_team_0,其中设备_实验室_团队_0.registerEquipment 并存在(从设备.laboratory.team 中选择 1 作为设备_laboratory_team_1,其中设备_实验室_团队_1.person.id = ?1))

标签: java spring-data-jpa querydsl boolean-expression


【解决方案1】:

我明白了。我不得不使用 JPAExpression。这是正确的代码:

QEquipment equipment = QEquipment.equipment;
QTeam team = QTeam.team;
QLaboratory laboratory = QLaboratory.laboratory;

Predicate predicate = JPAExpressions
       .selectOne()
       .from(team)
       .where(equipment.laboratory.id.eq(laboratory.id),
              laboratory.id.eq(team.laboratory.id),
              team.registerEquipment.eq(Boolean.TRUE),
              team.person.id.eq(userService.getCurrentId())).exists();

现在它会同时过滤具有注册设备权限的实验室成员。

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2015-02-14
    • 2012-11-25
    • 2012-06-19
    相关资源
    最近更新 更多