Java Hibernate HQL 中的“哪里存在”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3672444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"where exists" in Hibernate HQL
提问by benhsu
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:
如何在 HQL 中编写“不存在”查询?我正在尝试获取一个 HQL 不存在查询,该查询返回与此 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))
My mapping files are below:
我的映射文件如下:
<!-- 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>
I tried the following tag in my School.hbm.xml file
我在 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>
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!
谢谢!
采纳答案by Pascal Thivent
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:
您的命名查询无效(school_id
不是Student
实体的属性),这会阻止SessionFactory
被实例化。您需要考虑对象和关联,而不是列。试试这个:
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
- Hibernate 核心参考指南
回答by xcut
Try this:
尝试这个:
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