简体   繁体   English

如何用JPA 2.0标准以M:N关系表示?

[英]How to express not in M:N relation with JPA 2.0 Criteria?

How to express a query with JPA 2.0 Criteria that should fetch all entities A that are not in relation (m:n) with an Entity B. 如何使用JPA 2.0标准表达查询,该标准应获取与实体B不相关(m:n)的所有实体A。

Domain Model: 领域模型:

@Entity class A{
  @Id int id;
  @ManyToMany List<B> disableForB;
}

@Entity class B{
  @Id int id;
}

The problem related to this domain model is: How to find all A that are not in relation disableForB for an specific B ? 与该域模型相关的问题是:对于特定B ,如何查找与disableForB不相关的所有A

I do not have environment to test query available at the moment, so following can contain some mistake. 我目前没有环境可以测试可用的查询,因此以下操作可能会包含一些错误。 This is anyway doable with isNotMember . 无论如何,这可以通过isNotMember实现

B b ...
CriteriaQuery<Aa> cq = cb.createQuery(A.class);
ParameterExpression<B> param = cb.parameter(B.class);
Root<A> a = cq.from(A.class);
Expression<Collection<B>> disabledB = a.get("disableForB");
Predicate pred = cb.isNotMember(param, disabledB);
cq.select(a);
cq.where(pred);
em.createQuery(cq).setParameter(param, b).getResultList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM