Java 排除JPQL为“不在”的项目

Java 排除JPQL为“不在”的项目,java,hibernate,jpa,jpql,Java,Hibernate,Jpa,Jpql,我想通过以下JPQL查询排除带有特定标记的项目: select distinct i from Item i join i.tags t where t not in (:excludedTags) 如果一个项目只有一个标记,且该标记位于excludedTags列表中,则该选项有效。但是,如果该项目上有任何其他标记,它仍然会被选中 模型的相关部分: @Entity class Tag { @ManyToMany(mappedBy="tags") var items } @Entity

我想通过以下JPQL查询排除带有特定标记的项目:

select distinct i from Item i join i.tags t where t not in (:excludedTags)
如果一个项目只有一个标记,且该标记位于excludedTags列表中,则该选项有效。但是,如果该项目上有任何其他标记,它仍然会被选中

模型的相关部分:

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

@Entity
class Item {
  @ManyToMany
  var tags
}

如何使用JPQL排除包含任何已排除标记的项?

查询应类似于以下内容:

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)

非常感谢。它作为一个轻微的变体工作:。。。不存在从项目i2中选择标记加入i2.tags标记,其中i2.id=i.id,标记入:excludedTags。据我所知,由于一个hibernate错误,ExcludedTag周围的括号是必需的。该错误已在较新版本中修复。我使用的是4.1.9,不是那么旧。