[英]JPA query not in subquery relationship table
I'd like to know how to make a query in JPA where content is not inside a "third table" responsible to make relationship manyToMany. 我想知道如何在JPA中进行查询,该查询中的内容不在“第三表”中,该“第三表”负责使多对多关系。
example: 例:
SELECT id FROM A WHERE id NOT IN (SELECT id FROM A_B WHERE idB = @id)
In this case I have Table A, Table B, and the relationShip manytomany A_B 在这种情况下,我有表A,表B和关系Ship manytomany A_B
I tried with CriteriaQuery and subquery, but it didn't work. 我尝试使用CriteriaQuery和子查询,但是没有用。 Actually, it returned none result.
实际上,它没有返回任何结果。
Does somebody have any example for this case? 有人对此事有例子吗?
I got my solution! 我找到了解决方案!
I don't know if it is completely correct, so, if someone see an error, please let me know. 我不知道它是否完全正确,因此,如果有人看到错误,请告诉我。
The end query, created by JPA become this: 由JPA创建的最终查询变为:
select maingroup0_.GroupId as GroupId1_, maingroup0_.idGroupAttendanceType as idGroupA2_1_, maingroup0_.name as name1_ from TUnPbxGroup maingroup0_ where maingroup0_.GroupId not in (select maingroup1_.GroupId from TUnPbxGroup maingroup1_ inner join THolidayGroup listholida2_ on maingroup1_.GroupId=listholida2_.GroupId inner join THoliday holiday3_ on listholida2_.IdHoliday=holiday3_.IdHoliday where holiday3_.IdHoliday=2)
The code: 编码:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MainGroup> query = cb.createQuery(MainGroup.class);
Root<MainGroup> root = query.from(MainGroup.class);
query.select(root);
Subquery<Long> subquery = query.subquery(Long.class);
Root<MainGroup> subRoot = subquery.from(MainGroup.class);
subquery.select(subRoot.<Long>get("id"));
Join<Holiday, Holiday> maingroups = subRoot.join("listHolidays");
subquery.where(cb.equal(maingroups.get("id"), holidayId));
query.where(cb.not(cb.in(root.get("id")).value(subquery)));
TypedQuery<MainGroup> typedQuery = em.createQuery(query);
List<MainGroup> result = typedQuery.getResultList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.