Search code examples
javaspring-data-jpaquerydslboolean-expression

Boolean Expression for two conditions of an element of a list


I'm using QueryDSL here to build a predicate, the problem is that this predicate requires two AND conditions and I'm stuck here because I don't know how I'm gonna apply the second condition.

This is the scenario:

I have an equipment, this equipment belongs to a laboratory and the laboratory has a team. Some members of this laboratory team can register new equipments and some don't, this is defined by a boolean attribute "registerEquipment". So my predicate must have these condidtions:

*A user must be member of a laboratory team and has the attribute "registerEquipment" setted true to see this equipment . I tried the following code:

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

But is not working because it's executing a condition of registerEquipment first then the teamMember. I would like to know how could i execute both conditions for a team member at the same time.


Solution

  • I got it. I had to use a JPAExpression. This is the correct code:

    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();
    
    

    Now it's gonna filter at the same time the lab member who has permission to register an equipment.