对于以下对象:

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


如果我执行以下HQL查询,它将完成而没有错误,并返回正确的结果:

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


但是,我不明白这是如何工作的:

where :userId in elements(ruleEntity.users)


对我来说,这是检查是否在集合ruleEntity.users(这是UserEntitys的列表)中是否存在userID(这是UUID),所以我会因为在UUID对象的UserEntity对象列表中检查而失败了?

最佳答案

它选择所有具有用户列表的ruleEntities,并且至少一个用户列表包含:userId参数

用SQL术语

FROM rule_entity
WHERE :userId in (select user_id
                  from rule_entity_users
                  where rule_entity.rule_id=rule_entity_users.rule_id)

08-05 11:59