开发者

Case statement with nested select in HQL

I've problem to create a HQL statement for this SQL (Oracle).

SELECT 
 CASE WHEN Col开发者_如何学Pythonumn1 = 'VALUE1' THEN (
  SELECT COL2 FROM Table1
 ) ELSE 
 Column3
END
 FROM TABLE2 WHERE Columnx = 'something that is unique'

The HQL look more like the same. The problem is in that nested select in the case.

The error:

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) 

For 99% the Table1 and Table2 mappings are in the context.

Any advice will be appreciate.


Does Table1 have only one record ? if no then selecting from it inside the case will not work , try to join between table1 and table2 , like this :

    SELECT 
     CASE WHEN Column1 = 'VALUE1' THEN (
     COL2
     ) ELSE 
     Column3
    END AS CASE1
     FROM TABLE2 , Table1 
    WHERE Columnx = 'something that is unique'

if table2 , tabl3 need to be joined , then don't forget to do so , if not than a cartesian join will happen here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜