2013-03-13 54 views
0

我要排除加有與下面的JPQL查詢特定標籤的物品:排除項目與JPQL「不」

select distinct i from Item i join i.tags t where t not in (:excludedTags) 

它的工作原理如果item只有一個tagtagexcludedTags名單。但是如果在item上還有其他tag,它仍會被選中!

模型的相關部分:

@Entity 
class Tag { 
    @ManyToMany(mappedBy="tags") 
    var items 
} 

@Entity 
class Item { 
    @ManyToMany 
    var tags 
} 

我怎樣才能排除有JPQL排除任何標籤的物品?

回答

1

查詢應該是這樣的,而不是:

select distinct i from Item i where not exists (
    select t from Item i2 
    join i2.tags tag 
    where i2.id = i.id 
    and tag.id in :excludedTags) 
+0

謝謝。它作爲一個輕微的變體:'...不存在(從項目i2中選擇標籤加入i2.tags標籤,其中i2.id = i.id和標籤在(:excludedTags))''中。據我所知,由於存在一個休眠錯誤,需要'excludedTags'附近的括號。 – deamon 2013-03-14 08:35:36

+0

該bug已經在更新版本中得到修復。 – 2013-03-14 08:58:47

+0

我使用的是4.1.9,不是那麼古老。 – deamon 2013-03-14 10:21:35