From 920e3cf2ab3bae4875e447a6651af0ac58224b91 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Wed, 2 Apr 2014 14:44:06 -0500 Subject: [PATCH] HHH-6747 - ClassCastException when rendering SimpleCaseExpression --- .../expression/SimpleCaseExpression.java | 17 ++-- .../simplecase/BasicSimpleCaseTest.java | 99 +++++++++++++++++++ 2 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SimpleCaseExpression.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SimpleCaseExpression.java index 7dc4696898..d7dcb43ce5 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SimpleCaseExpression.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SimpleCaseExpression.java @@ -54,15 +54,15 @@ public class SimpleCaseExpression private Expression otherwiseResult; public class WhenClause { - private final C condition; + private final LiteralExpression condition; private final Expression result; - public WhenClause(C condition, Expression result) { + public WhenClause(LiteralExpression condition, Expression result) { this.condition = condition; this.result = result; } - public C getCondition() { + public LiteralExpression getCondition() { return condition; } @@ -99,7 +99,10 @@ public class SimpleCaseExpression } public SimpleCase when(C condition, Expression result) { - WhenClause whenClause = new WhenClause( condition, result ); + WhenClause whenClause = new WhenClause( + new LiteralExpression( criteriaBuilder(), condition ), + result + ); whenClauses.add( whenClause ); adjustJavaType( result ); return this; @@ -141,10 +144,10 @@ public class SimpleCaseExpression public String render(RenderingContext renderingContext) { StringBuilder caseExpr = new StringBuilder(); caseExpr.append( "case " ) - .append( ( (Renderable) getExpression() ).render( renderingContext ) ) - .append( ' ' ); + .append( ( (Renderable) getExpression() ).render( renderingContext ) ); for ( WhenClause whenClause : getWhenClauses() ) { - caseExpr.append( ( (Renderable) whenClause.getCondition() ).render( renderingContext ) ) + caseExpr.append( " when " ) + .append( whenClause.getCondition().render( renderingContext ) ) .append( " then " ) .append( ( (Renderable) whenClause.getResult() ).render( renderingContext ) ); } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java new file mode 100644 index 0000000000..d8cc07b679 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/simplecase/BasicSimpleCaseTest.java @@ -0,0 +1,99 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2014, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.jpa.test.criteria.simplecase; + +import javax.persistence.Entity; +import javax.persistence.EntityManager; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Path; +import javax.persistence.criteria.Root; + +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; + +import org.junit.Test; + +import static javax.persistence.criteria.CriteriaBuilder.SimpleCase; + +/** + * Mote that these are simply performing syntax checking (can the criteria query + * be properly compiled and executed) + * + * @author Steve Ebersole + */ +public class BasicSimpleCaseTest extends BaseEntityManagerFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {Customer.class}; + } + + @Test + public void testCaseInOrderBy() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + CriteriaBuilder builder = em.getCriteriaBuilder(); + + CriteriaQuery query = builder.createQuery( Customer.class ); + Root root = query.from( Customer.class ); + query.select( root ); + + Path emailPath = root.get( "email" ); + SimpleCase orderCase = builder.selectCase( emailPath ); + orderCase = orderCase.when( "test@test.com", 1 ); + orderCase = orderCase.when( "test2@test.com", 2 ); + + query.orderBy( builder.asc( orderCase.otherwise( 0 ) ) ); + + em.createQuery( query ); + + } + + @Entity(name = "Customer") + @Table(name = "customer") + public static class Customer { + private Integer id; + private String email; + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + } +}