Uploaded image for project: 'Hibernate ORM'
  1. HHH-8124

Criteria selectCase() query with literals doesn't work

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 4.2.0.Final, 4.3.0.Beta1
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true

      Description

      (There doesn't seem to be any test for CriteriaBuilder#selectCase().)

      This fails:

      Root<User> u = criteria.from(User.class);
      criteria.select(
          cb.selectCase()
              .when(
                  cb.equal(u.get("username"), "Foo"),
                  "Bar"
              ).otherwise("Baz")
      );
      

      The actual query/entity mapping doesn't matter, any selectCase() expression with literals fails:

      java.lang.ClassCastException: org.hibernate.hql.internal.ast.tree.ParameterNode cannot be cast to org.hibernate.hql.internal.ast.tree.SelectExpression
      	at org.hibernate.hql.internal.ast.tree.CaseNode.getFirstThenNode(CaseNode.java:43)
      	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.<init>(HQLQueryPlan.java:105)
      	at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
      	at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
      	at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:270)
      	at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:221)
      	at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
      	at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:538)
      	at org.hibernate.jpa.criteria.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:376)
      	at org.hibernate.jpa.criteria.compile.CriteriaCompiler.compile(CriteriaCompiler.java:128)
      	at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:679)
      

      The rendered criteria query is using parameters for the literals:

      select case when generatedAlias0.username=:param0 then :param1 else :param2 end from User as generatedAlias0
      

      The JPQL equivalent with simple literals and no parameters works:

      select case when u.username='Foo' then 'Bar' else 'Baz' end from User as u
      

        Attachments

          Issue links

            Activity

              People

              • Votes:
                3 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: