diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/CaseLiteralExpression.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/CaseLiteralExpression.java new file mode 100644 index 0000000000..d56acff1ef --- /dev/null +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/CaseLiteralExpression.java @@ -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 . + */ +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 extends LiteralExpression { + + public CaseLiteralExpression(CriteriaBuilderImpl criteriaBuilder, Class 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() ) + + ')'; + } +} diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SearchedCaseExpression.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SearchedCaseExpression.java index 2750da87d1..8e978c2703 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SearchedCaseExpression.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SearchedCaseExpression.java @@ -57,20 +57,20 @@ public class SearchedCaseExpression public SearchedCaseExpression( CriteriaBuilderImpl criteriaBuilder, Class javaType) { - super( criteriaBuilder, javaType); + super( criteriaBuilder, javaType ); this.javaType = javaType; } public Case when(Expression condition, R result) { - return when( condition, buildLiteral(result) ); + return when( condition, buildLiteral( result ) ); } - @SuppressWarnings({ "unchecked" }) + @SuppressWarnings({"unchecked"}) private LiteralExpression buildLiteral(R result) { final Class type = result != null ? (Class) result.getClass() : getJavaType(); - return new LiteralExpression( criteriaBuilder(), type, result ); + return new CaseLiteralExpression( criteriaBuilder(), type, result ); } public Case when(Expression condition, Expression result) { @@ -80,7 +80,7 @@ public class SearchedCaseExpression return this; } - @SuppressWarnings({ "unchecked" }) + @SuppressWarnings({"unchecked"}) private void adjustJavaType(Expression exp) { if ( javaType == null ) { javaType = (Class) exp.getJavaType(); @@ -88,7 +88,7 @@ public class SearchedCaseExpression } public Expression otherwise(R result) { - return otherwise( buildLiteral(result) ); + return otherwise( buildLiteral( result ) ); } public Expression otherwise(Expression result) { 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 43e4b36a52..dcd1a27fed 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 @@ -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 CASE expression in the form
@@ -78,7 +79,7 @@ public class SimpleCaseExpression
 		final Class type = result != null
 				? (Class) result.getClass()
 				: getJavaType();
-		return new LiteralExpression( criteriaBuilder(), type, result );
+		return new CaseLiteralExpression( criteriaBuilder(), type, result );
 	}
 
 	public SimpleCase when(C condition, Expression result) {
diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/selectcase/SelectCaseTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/selectcase/SelectCaseTest.java
index a24c9ba016..7d5b1d26f8 100644
--- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/selectcase/SelectCaseTest.java
+++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/selectcase/SelectCaseTest.java
@@ -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;