"où existe" en hibernation HQL

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

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 mappage 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é l'étiquette suivante dans mon école.hbm.fichier 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 eu cette trace de la 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 laisser moi ce que je fais mal?

Merci!

31
demandé sur Alex Barnes 2010-09-09 02:41:34

2 réponses

Votre requête n'est pas valide (school_id n'est pas une propriété de l' Student entité) qui empêche l' SessionFactory d'être instanciée. Vous devez penser objet et associations, pas 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)
)

Références

45
répondu Pascal Thivent 2010-09-08 23:26:27

essaye ceci:

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
répondu xcut 2010-09-08 22:53:00