web-dev-qa-db-fra.com

"où existe" dans Hibernate HQL

Comment puis-je écrire une requête "n'existe pas" dans HQL? J'essaie d'obtenir une requête HQL non existante qui renvoie les mêmes résultats que cette requête Oracle SQL:

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

Mes fichiers de cartographie sont ci-dessous:

  <!-- 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>
  <!-- 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>

J'ai essayé la balise suivante dans mon fichier School.hbm.xml

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

et j'ai cette trace de pile

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)

Quelqu'un peut-il me faire savoir ce que je fais mal?

Je vous remercie!

33
benhsu

Votre requête nommée n'est pas valide (school_id n'est pas une propriété de l'entité Student), ce qui empêche l'instanciation de SessionFactory. Vous devez penser à l'objet et aux associations, pas aux colonnes. Essayez plutôt ceci:

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

Les références

49
Pascal Thivent

Essaye ça:

from School s where (select count(st) from Student st
  where st.school_id=s.id and st.status.id not in (0,1,2,3,4)) = 0
4
xcut