Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:46:28+00:00 2026-06-16T01:46:28+00:00

I have two tables named as table1 ,table2.Both the tables are having same no

  • 0

I have two tables named as table1 ,table2.Both the tables are having same no of fields.There is no relation between these two tables.My requirement is I want all the records in table1 which are not there in table2. So I have written a query using Criteria API. But it is not giving the correct result. As I am new to this JPA and criteria API, can any one point me where I am doing wrong.The below code I am using to do this.

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 Query :

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'; 

I need the JPA criteria API query for the above mentioned MySQL query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:46:29+00:00Added an answer on June 16, 2026 at 1:46 am

    You can try the below code with Criteria API. I haven’t tried, but you can try modifying the code accordingly.

    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();
    

    Also, you can try below JPQL query, which is easier to understand, alter & debug.

    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); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables table1 and table2 each having a column named email along
I have two tables table1 = records, table2 = duplicates. Both tables contain a
I have two tables, both named as say, Employee in two different schema HR
So I have two tables. Table 1 is named 'appointment' with the following fields:
I have two Tables say Table-A and Table-B having same table structure (means number
I have two tables table1 +-------+-------+ | NAME | PRICE | +-------+-------+ | ITEM1
I have the following two tables: Table1 ---------- ID Name 1 A 2 B
here is my problem. I have two tables, Table1 - id | Name |
One doubt in MSSQL. There are two tables in a databases. Table 1 named
I have two tables: Table_A name1 create_date Table_B name2 keep_time name1 is something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.