【问题标题】:JQL Many to Many select where :item not in itemsJQL 多对多选择 where :item not in items
【发布时间】:2015-03-29 14:04:10
【问题描述】:

当我们有这个结构时:

@Entity
public class A {

....

    @ManyToMany(mappedBy = "a")
    public Set<B> getB() {
        return b;
    }

@Entity
public class B {

...

    @ManyToMany
    public Set<A> getA() {
        return a;
    }

我们如何查询实体 A 以获取给定 b 值不在Set&lt;B&gt; 中的行?我的意思是,获取所有在Set&lt;B&gt; 中没有给出 b 的 A。

我正在尝试这个解决方案:

from A where :bInstance not in b

select a from A a where a not in (b.a from B b where b.name = :name)

select a from A a where a not exists (b.a from B b where b.name = :name)

但没有运气。

我知道我可以通过查询所有 A 然后对其进行迭代和过滤来做到这一点,但我想在不查询 db 中的所有行的情况下做到这一点。

【问题讨论】:

    标签: hibernate jakarta-ee jpa


    【解决方案1】:

    有一个更好的方法,使用member of 运算符:

    select a from A a where :bInstance not member of a.b
    

    【讨论】:

      猜你喜欢
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 2015-04-10
      • 1970-01-01
      相关资源
      最近更新 更多