We're updating the issue view to help you get more done. 

Criteria selectCase() query with literals doesn't work

Description

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

This fails:

1 2 3 4 5 6 7 8 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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:

1 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:

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

Environment

None

Status

Assignee

Unassigned

Reporter

Christian Bauer

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

4.3.0.Beta1
4.2.0.Final

Priority

Major