HHH-9731 : Wrap Criteria CaseStatement params in string casts

This commit is contained in:
Andrea Boriero 2015-09-02 10:47:58 +01:00
parent 9a6ecb914e
commit 0aca04ff7a
4 changed files with 43 additions and 7 deletions

View File

@ -0,0 +1,31 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.criteria.expression;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.compile.RenderingContext;
import org.hibernate.jpa.criteria.expression.function.CastFunction;
/**
* @author Andrea Boriero
*/
public class CaseLiteralExpression<T> extends LiteralExpression<T> {
public CaseLiteralExpression(CriteriaBuilderImpl criteriaBuilder, Class<T> type, T literal) {
super( criteriaBuilder, type, literal );
}
@Override
public String render(RenderingContext renderingContext) {
// wrapping the result in a cast to determine the node type during the antlr hql parsing phase
return CastFunction.CAST_NAME + '(' +
super.render( renderingContext ) +
" as " +
renderingContext.getCastType( getJavaType() ) +
')';
}
}

View File

@ -57,20 +57,20 @@ public class SearchedCaseExpression<R>
public SearchedCaseExpression(
CriteriaBuilderImpl criteriaBuilder,
Class<R> javaType) {
super( criteriaBuilder, javaType);
super( criteriaBuilder, javaType );
this.javaType = javaType;
}
public Case<R> when(Expression<Boolean> condition, R result) {
return when( condition, buildLiteral(result) );
return when( condition, buildLiteral( result ) );
}
@SuppressWarnings({ "unchecked" })
@SuppressWarnings({"unchecked"})
private LiteralExpression<R> buildLiteral(R result) {
final Class<R> type = result != null
? (Class<R>) result.getClass()
: getJavaType();
return new LiteralExpression<R>( criteriaBuilder(), type, result );
return new CaseLiteralExpression<R>( criteriaBuilder(), type, result );
}
public Case<R> when(Expression<Boolean> condition, Expression<? extends R> result) {
@ -80,7 +80,7 @@ public class SearchedCaseExpression<R>
return this;
}
@SuppressWarnings({ "unchecked" })
@SuppressWarnings({"unchecked"})
private void adjustJavaType(Expression<? extends R> exp) {
if ( javaType == null ) {
javaType = (Class<R>) exp.getJavaType();
@ -88,7 +88,7 @@ public class SearchedCaseExpression<R>
}
public Expression<R> otherwise(R result) {
return otherwise( buildLiteral(result) );
return otherwise( buildLiteral( result ) );
}
public Expression<R> otherwise(Expression<? extends R> result) {

View File

@ -16,6 +16,7 @@ import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.ParameterRegistry;
import org.hibernate.jpa.criteria.Renderable;
import org.hibernate.jpa.criteria.compile.RenderingContext;
import org.hibernate.jpa.criteria.expression.function.CastFunction;
/**
* Models what ANSI SQL terms a simple case statement. This is a <tt>CASE</tt> expression in the form<pre>
@ -78,7 +79,7 @@ public class SimpleCaseExpression<C,R>
final Class<R> type = result != null
? (Class<R>) result.getClass()
: getJavaType();
return new LiteralExpression<R>( criteriaBuilder(), type, result );
return new CaseLiteralExpression<R>( criteriaBuilder(), type, result );
}
public SimpleCase<C, R> when(C condition, Expression<? extends R> result) {

View File

@ -29,10 +29,14 @@ import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import org.hibernate.jpa.criteria.expression.ConcatExpression;
import org.hibernate.jpa.criteria.expression.ExpressionImpl;
import org.hibernate.jpa.criteria.expression.function.AbsFunction;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;