HHH-6747 - ClassCastException when rendering SimpleCaseExpression

Conflicts:
	hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SimpleCaseExpression.java
This commit is contained in:
Steve Ebersole 2014-04-08 11:46:37 -04:00 committed by Brett Meyer
parent f1ddacda47
commit 8f416e146d
2 changed files with 104 additions and 7 deletions

View File

@ -54,15 +54,15 @@ public class SimpleCaseExpression<C,R>
private Expression<? extends R> otherwiseResult;
public class WhenClause {
private final C condition;
private final LiteralExpression<C> condition;
private final Expression<? extends R> result;
public WhenClause(C condition, Expression<? extends R> result) {
public WhenClause(LiteralExpression<C> condition, Expression<? extends R> result) {
this.condition = condition;
this.result = result;
}
public C getCondition() {
public LiteralExpression<C> getCondition() {
return condition;
}
@ -99,7 +99,7 @@ public class SimpleCaseExpression<C,R>
}
public SimpleCase<C, R> when(C condition, Expression<? extends R> result) {
WhenClause whenClause = new WhenClause( condition, result );
WhenClause whenClause = new WhenClause( new LiteralExpression<C>( criteriaBuilder(), condition ), result );
whenClauses.add( whenClause );
adjustJavaType( result );
return this;
@ -141,10 +141,10 @@ public class SimpleCaseExpression<C,R>
public String render(CriteriaQueryCompiler.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 ) );
}

View File

@ -0,0 +1,97 @@
/*
* 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.CriteriaBuilder.SimpleCase;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
/**
* 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<Customer> query = builder.createQuery( Customer.class );
Root<Customer> root = query.from( Customer.class );
query.select( root );
Path<String> emailPath = root.get( "email" );
SimpleCase<String, Integer> 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;
}
}
}