【发布时间】:2020-03-16 16:28:57
【问题描述】:
我想找到所有安装了“b”= 500 和“b”= 501 和“c”= 2 的“a”。
在SQL 我会这样做:
select a from table1 where id in (select info_id from table2 where b = '500')
intersect
select a from table1 where id in (select info_id from table2 where b = '501')
intersect
select a from table1 where id in (select info_id from table2 where id in (select ecu_id from table3 where c = '2'));
所以我会创建三个SQL 查询,然后取这三个查询的交集。
但我现在必须使用基于 JPA criteria 的查询,而不是原生的 SQL 或 JPQL。
我们可以执行三个不同的基于JPA criteria 的查询:
返回一个“a”列表,其中“b”= 500,
返回一个“a”列表,其中“b”= 501 和
返回一个“a”列表,其中“c”=2。
然后过滤所有三个返回列表中出现的所有“a”。但这会花费太长时间,我们的数据库包含数百万个“a”条目......
【问题讨论】:
-
我认为这个链接可以帮助你。 objectdb.com/java/jpa/query/jpql/…
-
您可以创建 3 个
subqueries,然后使用criteriaBuilder.exists(subquery)谓词。更多信息在这里logicbig.com/tutorials/java-ee-tutorial/jpa/… -
谢谢你们,我现在可以在 SQL 中将
intersect命令替换为三个与where和exists连接的subqueries。我现在将尝试将其翻译为criteriaBuilder并发布解决方案,如果可行的话...... -
我还没有 JPA Criteria Queria 解决方案,但是带有 Exist Subqueries 的 SQL 现在正在运行:stackoverflow.com/questions/60739497/…