我在为这个SQL(Oracle)创建一个HQL语句时遇到了问题。
SELECT
CASE WHEN Column1 = 'VALUE1' THEN (
SELECT COL2 FROM Table1
) ELSE
Column3
END
FROM TABLE2 WHERE Columnx = 'something that is unique'
HQL看起来更像是一样的。问题在于案例中的嵌套选择。
错误:
java.lang.NullPointerException
at org.hibernate.hql.ast.tree.CaseNode.getDataType(CaseNode.java:40)
at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:165)
at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:831)
at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:619)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:672)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:288)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
对于99%,Table1和Table2映射都在上下文中。
任何建议都会受到赞赏。
答案 0 :(得分:0)
Table1只有一条记录吗?如果没有,那么在案例中选择它将不起作用,尝试在table1和table2之间加入,如下所示:
SELECT
CASE WHEN Column1 = 'VALUE1' THEN (
COL2
) ELSE
Column3
END AS CASE1
FROM TABLE2 , Table1
WHERE Columnx = 'something that is unique'
如果table2,tabl3需要加入,那么不要忘记这样做,如果不是这里会发生笛卡尔联接。