Question

For the following objects:

class UserEntity {
  UUID id;
  private List<TeamEntity> teams = new ArrayList<TeamEntity>();
  private List<RuleEntity> rules = new ArrayList<RuleEntity>();
  ...

}

class RuleEntity {
  UUID id;
  private TeamEntity ownerTeam;
  private List<UserEntity> users = new ArrayList<UserEntity>(); 
  ...
}

Class TeamEntity {
  UUID id;
  private List<UserEntity> users = new ArrayList<UserEntity>();
  private List<RuleEntity> rules = new ArrayList<RuleEntity>();
}

If I execute the following HQL query it completes without error and returns the correct results:

select count(ruleEntity) from RuleEntity ruleEntity where :userId in elements(ruleEntity.users) and ruleEntity.ownerTeam.id = :teamId

However, I don't understand how this bit works:

where :userId in elements(ruleEntity.users)

To me this is checking if userID (which is a UUID) exists within the collection ruleEntity.users, which is a List of UserEntitys, so I would have though this would failed since its checking in a list of UserEntity objects for a UUID object?

Was it helpful?

Solution

It selects all ruleEntities which has list of users and at least one of the users list contains the :userId param

In SQL terms

FROM rule_entity
WHERE :userId in (select user_id
                  from rule_entity_users
                  where rule_entity.rule_id=rule_entity_users.rule_id)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top