2014-11-02 52 views
0

我有一個實體,有標籤的集合JPA JPQL:與列表屬性選擇實體

@ManyToMany 
private Set<Tag> listeTag; 

我想編寫一個查詢將返回我的實體的列表,它包含的參數,而不是通過所有標籤就像這樣。

select distinct entity from Entity entity where and entity.listeTag in :listeTag 

是否有listeTag兩個標籤我只希望這至少有兩個標籤實體。

回答

0
select e from SomeEntity e where :numberOfTagsInSet = 
    (select count(tag.id) from SomeEntity e2 
    join e2.listeTag tag 
    where e.id = e2.id 
    and tag.id in :setOfTagIds) 

這應該做的伎倆。

+0

我不明白你的查詢,但它的作品!非常感謝 – sepointes 2014-11-02 15:52:21

+1

其實很簡單。假設您要查找的標籤是foo和bar。內部查詢計算f2或bar的e2標籤數量。如果結果是2,那麼你知道e2有標籤foo和bar。外部查詢返回所有的實體數量是2。 – 2014-11-02 15:55:19

+0

ok!我沒有想到要用數字來嘗試。我盡我所能兩天。再次感謝 – sepointes 2014-11-02 16:02:46