2013-01-02 23 views
1

我有一個GrantedRole類,允許特定的角色在一個或多個Site的授予,如:JPA/HQL - 查找與包含一個或多個特定的元素一個一對多的關係行

public class GrantedRole { 
    private RoleType role; 

    private User granter; 
    private User grantee; 
    private Collection<Site> grantSites; 

    @Column(nullable = false) 
    public UserRoleType getRole() { 
     return role; 
    } 

    @ManyToOne(optional = false, fetch=FetchType.LAZY) 
    public User getGranter() { 
     return granter; 
    } 

    @ManyToOne(optional = false, fetch=FetchType.LAZY) 
    public User getGrantee() { 
     return grantee; 
    } 

    @ManyToMany 
    public Collection<Site> getGrantSites() { 
     return grantSites; 
    } 
} 

我試圖拿出的是,會發現所有的GrantedRole「一個特定的授予者已經給出了S作一個或多個指定Site」在他們grantSites系列S查詢。因此,像:

SELECT g FROM GrantedRole g WHERE g.granter = :granter AND 
    g.grantSites [contains at least one of] (:sites) 

...但我不知道什麼語法使用[contains at least one of],或者如果這甚至有可能使用JPA/HQL。有什麼建議麼?

回答

1

我能夠通過修改答案,使這項工作在這裏找到:

Checking the Intersection of Two Collections via HQL

具體做法是:

SELECT DISTINCT g FROM GrantedRole g, Site s WHERE g.granter = :granter 
    AND s IN (:sites) AND s IN ELEMENTS(g.grantSites) 
    ORDER BY g.grantee.firstName ASC, g.grantee.lastName ASC