hibernate hql case when 子查询报java.lang.NullPointerException错误
Posted 空白-键
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate hql case when 子查询报java.lang.NullPointerException错误相关的知识,希望对你有一定的参考价值。
hql语句大致如下
select t, case when t.targetType=0 then
(select a.name from MarketApp a where a.id = t.target )
else '' end
from ThemeComponent t where t.theme.id = 1
jsp报如下错误
java.lang.NullPointerException
at org.hibernate.hql.internal.ast.tree.CaseNode.getDataType(CaseNode.java:39)
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:152)
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:859)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:649)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:663)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1777)
后台控制台报如下错误
[2014-06-12 09:29:37 ERROR] [http-8080-3] org.hibernate.hql.internal.ast.ErrorCounter.reportError(50) | <AST>:0:0: unexpected AST node: query
<AST>:0:0: unexpected AST node: query
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1363)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.caseExpr(HqlSqlBaseWalker.java:5074)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.arithmeticExpr(HqlSqlBaseWalker.java:3199)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2332)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2145)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1451)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
org.hibernate.hql.internal.ast.tree.CaseNode.getDataType(CaseNode.java:39) 可以看出 报错是因为hibernate不知道子查询返回的是什么类型,所以报错,那就强制让它知道,比如本例子返回的是字符串类型,所以可以改成如下
select t, case when t.targetType=0 then
((select a.name from MarketApp a where a.id = t.target ) || '')
else '' end
from ThemeComponent t where t.theme.id = 1
以上是关于hibernate hql case when 子查询报java.lang.NullPointerException错误的主要内容,如果未能解决你的问题,请参考以下文章
hibernate的hql语句不支持 count(case...when ....else..)?