2014-05-02 82 views
0

以下對象:爲什麼HQL查詢看起來不應該這樣工作?

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) 

對我來說這是檢查是否用戶ID(這是一個UUID)集合ruleEntity.users中存在,這是UserEntitys的名單,S o我會雖然這將失敗,因爲它在一個UUID對象的UserEntity對象列表中檢查?

回答

1

它選擇具有用戶和用戶的至少一個列表中的所有ruleEntities列表中包含:用戶id PARAM

在SQL方面

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