我有兩個表,分別命名爲table1,table2.兩個表都具有相同的no域。這兩個表之間沒有關係。我的要求是我希望所有記錄在table1中沒有在table2中。所以我寫了一個使用Criteria API的查詢。但它沒有給出正確的結果。由於我對這個JPA和標準API不熟悉,任何人都可以指出我在哪裏做錯了。我正在使用下面的代碼來執行此操作。在jpa標準api中使用NOT EXISTS構建查詢
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查詢:
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標準API查詢。
但你是如何區分這些表之間的數據,指定標準/柱等 –
嗨非常感謝給我給我原來的實際MySQL查詢Question.Plz查看一下,如果可能的話給出答覆我的Jpa標準API查詢相同的MySQL查詢。 – aaaa