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 4603220
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:06:47+00:00 2026-05-22T00:06:47+00:00

I am trying to create a not in query using hibernate Criteria. What I

  • 0

I am trying to create a “not in” query using hibernate Criteria. What I am trying to get is all the persons that don’t know this language, so I have an entity that looks like:

public class Person {
    ...
    private List<Language> languages;
    ...
}

public class Language {

    public Long id;
    public String label;
}

and my criteria code that looks like

Criteria cr = createCriteriaForPerson() // created criteria 

cr.createCriteria("languages").add(Restrictions.not(Restrictions.in("id", values)));

this returns all the persons including the ones that have the language.

If I try to search for the persons that have know the specific language, then the equivalent query returns the correct results

Criteria cr = createCriteriaForPerson() // created criteria 

cr.createCriteria("languages").add(Restrictions.in("id", values));

What could be the problem?

Thanks
Makis

  • 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-05-22T00:06:47+00:00Added an answer on May 22, 2026 at 12:06 am

    I’m pretty sure that you cannot express this query in Criteria API without using sqlRestriction.

    Naive approach produces a query like this:

    select p from Person p join p.languages l where l.id not in :values
    

    It’s obviously not what you want since other languages are still selected.

    To express the desired query you need a complex join

    select p from Person p left join p.languages l with l.id in :values where l is null
    

    or subquery

    select p from Person p where not exists 
        (select l from Language l where l in elements(p.languages) and l.id in :values)
    

    Since Criteria API doesn’t support joins with extra conditions, the only way to express this query is to use sqlRestriction with a subqery (exact form of the subqery depends on your database schema):

    cr.add(Restrictions.sqlRestriction(
        "not exists (select ... where l.person_id = {alias}.id and ...)"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a rails web app that does not use ActiveRecord framework
I'm not sure how to describe this but I'm trying to create a base
I am trying to create a blog using blogspot. It seems it is not
I'm trying to create a login system using PHP, and I'm not quite sure
I am trying to use a MapSqlParameterSource to create a query using a Like
I'm trying to create a query in c# for Windows Mobile 7 using linq
I am trying to create a completely dynamic way to query entity framework using
I am trying to create a query in MS SQL 2005 that will return
I'm trying to run a simple Hibernate application, but I get this error: org.hibernate.exception.SQLGrammarException:
I am trying to construct an sql query using a while loop that increments

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.