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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:39:44+00:00 2026-05-16T16:39:44+00:00

How can I write a "not exists" query in HQL? I am trying to

  • 0

How can I write a "not exists" query in HQL? I am trying to get an HQL not exists query which returns the same results as this Oracle SQL query:

    select *
    from SCHOOL a
    where not exists (select 1
    from STUDENT b
    where B.SCHOOL_ID=a.id
    and B.STATUS_ID not in (0,1,2,3,4))

My mapping files are below:

<!-- School.hbm.xml -->
    <class name="com.companyname.School"
             table="SCHOOL"
             >

      <!-- primary key ommitted -->
    <set name="students"
         cascade="all" fetch="select" lazy="false" >
        <key column="SCHOOL_ID" />
        <one-to-many class="com.companyname.Student" />
    </set>
    </class>

<!-- Student.hbm.xml -->
   <class
         name="com.companyname.Student"
         table="STUDENT"
         >

      <!-- primary key ommitted -->
     <many-to-one name="school" 
                  column="SCHOOL_ID"
                  class="com.companyname.School" fetch="join" lazy="false"/>

     <many-to-one name="status"
            class="com.companyname.Status" column="STATUS_ID" />
    </class>
<!-- Status.hbm.xml. This is a table of lookup data-->
   <class
         name="com.companyname.Status"
         table="LK_STATUS"
         mutable="false"
         >
      <id
            name="id"
            type="java.lang.Integer"
            column="ID"
            >
       </id>
      <property
            name="name"
            type="java.lang.String"
            column="NAME"
            not-null="true"
            unique="true"
            >
      </property>
   </class>

I tried the following tag in my School.hbm.xml file

    <query name="myQuery">
        <![CDATA[
        from School s where not exists from Student st
        where st.school_id=s.id and st.status.id not in (0,1,2,3,4)
        ]]>
    </query>

and I got this stack trace

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException
        at org.hibernate.hql.ast.HqlParser.negateNode(HqlParser.java:117)
        at org.hibernate.hql.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2378)
        at org.hibernate.hql.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2331)
        at org.hibernate.hql.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2296)
        at org.hibernate.hql.antlr.HqlBaseParser.expression(HqlBaseParser.java:2082)
        at org.hibernate.hql.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1858)
        at org.hibernate.hql.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:454)
        at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:708)
        at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
        at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
        at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:248)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
        at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
        at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
        at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:402)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:352)

Can somebody let me know what I’m doing wrong?

Thank you!

  • 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-16T16:39:45+00:00Added an answer on May 16, 2026 at 4:39 pm

    Your named query is not valid (school_id is not a property of the Student entity), which prevents the SessionFactory from being instantiated. You need to think object and associations, not columns. Try this instead:

    from School as s
    where not exists (
      from Student as st
      where st.school = s
      and st.status.id not in (0,1,2,3,4)
    )
    

    References

    • Hibernate Core Reference Guide
      • 14.13. Subqueries
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write bundles in Cocoa without much "fuzz" around it? I just
I trying to write a generic function which takes the function and number of
I'd like to know if we can write a short boolean test which return
OK, I know what you're thinking, "why write a method you do not want
Implementing a condition in template match <xsl:template match="a[!(img)and(not(@id))]"> I want to write a template
I'm trying to write a test for the Jasmine Test Framework which expects an
You can write //google.com instead of http://google.com and https://google.com where // is relative to
I can write an assertion message one of two ways. Stating success: assertEquals( objects
We can write CSS as the following types: Inline CSS Embedded CSS External CSS
I can write something, I'm asking if something is already built into jQuery.

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.