首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在jpa criteria api中使用NOT EXISTS构建查询

在jpa criteria api中使用NOT EXISTS构建查询
EN

Stack Overflow用户
提问于 2012-12-13 20:22:28
回答 1查看 27K关注 0票数 13

我有两个表名为table1,fields.There 2,这两个表都有相同的no,这两个tables.My之间没有关系,要求是我想要table1中的所有记录,这些记录在table2中不在那里。因此,我使用Criteria API编写了一个查询。但它没有给出正确的结果。因为我是新接触这个JPA和criteria,有谁能告诉我在我用来做这件事的代码下面做wrong.The的地方吗?

代码语言:javascript
运行
复制
CriteriaBuilder cb = mediationEntityManager.getCriteriaBuilder();
CriteriaQuery<Table1>  cq = cb.createQuery(Table1.class);
Root<Table1> table1 = cq.from(Table1.class);
cq.select(table1)

Subquery<Table2> subquery =  cq.subquery(Table2.class)
Root table2 = subquery.from(Table2.class)
subquery.select(table2)
cq.where(cb.not(cb.exists(subquery)))
TypedQuery<Table1> typedQuery = mediationEntityManager.createQuery(cq); 
List<Table1> resultList = typedQuery.getResultList();

MySQL查询:

代码语言:javascript
运行
复制
SELECT table1 
FROM   table1 table1 
WHERE  NOT EXISTS (SELECT table2 
                   FROM   table2 table2 
                   WHERE  table2.name = table1.name 
                          AND table2.education = table1.education 
                          AND table2.age = table1.age) 
       AND table1.name = 'san' 
       AND table1.age = '10'; 

对于上面提到的MySQL查询,我需要JPA查询。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-17 19:09:43

您可以使用Criteria API来尝试以下代码。我没有尝试过,但您可以尝试相应地修改代码。

代码语言:javascript
运行
复制
CriteriaBuilder cb = mediationEntityManager.getCriteriaBuilder();  
CriteriaQuery<Table1> query = cb.createQuery(Table1.class); 
Root<Table1> table1 =  query.from(Table1.class); 
query.select(table1);
//--  
Subquery<Table2> subquery = query.subquery(Table2.class); 
Root<Table2> table2 = subquery.from(Table2.class);  
subquery.select(table2);  
//--
List<Predicate> subQueryPredicates = new ArrayList<Predicate>(); 
subQueryPredicates.add(cb.equal(table1.get(Table1_.name), table2.get(Table2_.name)));
subQueryPredicates.add(cb.equal(table1.get(Table1_.age), table2.get(Table2_.age)));
subQueryPredicates.add(cb.equal(table1.get(Table1_.education), table2.get(Table2_.education)));
subquery.where(subQueryPredicates.toArray(new Predicate[]{})); 
//--
List<Predicate> mainQueryPredicates = new ArrayList<Predicate>(); 
mainQueryPredicates.add(cb.equal(table1.get(Table1_.name), "san");
mainQueryPredicates.add(cb.equal(table1.get(Table1_.age), "10");
mainQueryPredicates.add(cb.not(cb.exists(subquery))); 
//--
query.where(mainQueryPredicates.toArray(new Predicate[]{})); 
TypedQuery<Table1> typedQuery =  mediationEntityManager.createQuery(query); 
List<Table1> resultList = typedQuery.getResultList();

此外,您还可以尝试下面的JPQL查询,它更容易理解、更改和调试。

代码语言:javascript
运行
复制
SELECT t1 
FROM   table1 t1, 
       table2 t2 
WHERE  t1.name = 'san' 
       AND t1.age = '10' 
       AND (t2.name <> t1.name 
             AND t2.education <> t1.education 
             AND t2.age <> t1.age); 
票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13859780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档