Skip to content

Commit b9fce69

Browse files
committed
HHH-9731 : Wrap Criteria CaseStatement params in string casts
1 parent 7141bf8 commit b9fce69

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.jpa.criteria.expression;
8+
9+
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
10+
import org.hibernate.jpa.criteria.compile.RenderingContext;
11+
import org.hibernate.jpa.criteria.expression.function.CastFunction;
12+
13+
/**
14+
* @author Andrea Boriero
15+
*/
16+
public class CaseLiteralExpression<T> extends LiteralExpression<T> {
17+
18+
public CaseLiteralExpression(CriteriaBuilderImpl criteriaBuilder, Class<T> type, T literal) {
19+
super( criteriaBuilder, type, literal );
20+
}
21+
22+
@Override
23+
public String render(RenderingContext renderingContext) {
24+
// wrapping the result in a cast to determine the node type during the antlr hql parsing phase
25+
return CastFunction.CAST_NAME + '(' +
26+
super.render( renderingContext ) +
27+
" as " +
28+
renderingContext.getCastType( getJavaType() ) +
29+
')';
30+
}
31+
}

hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SearchedCaseExpression.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ public Expression<? extends R> getResult() {
5757
public SearchedCaseExpression(
5858
CriteriaBuilderImpl criteriaBuilder,
5959
Class<R> javaType) {
60-
super( criteriaBuilder, javaType);
60+
super( criteriaBuilder, javaType );
6161
this.javaType = javaType;
6262
}
6363

6464
public Case<R> when(Expression<Boolean> condition, R result) {
65-
return when( condition, buildLiteral(result) );
65+
return when( condition, buildLiteral( result ) );
6666
}
6767

68-
@SuppressWarnings({ "unchecked" })
68+
@SuppressWarnings({"unchecked"})
6969
private LiteralExpression<R> buildLiteral(R result) {
7070
final Class<R> type = result != null
7171
? (Class<R>) result.getClass()
7272
: getJavaType();
73-
return new LiteralExpression<R>( criteriaBuilder(), type, result );
73+
return new CaseLiteralExpression<R>( criteriaBuilder(), type, result );
7474
}
7575

7676
public Case<R> when(Expression<Boolean> condition, Expression<? extends R> result) {
@@ -80,15 +80,15 @@ public Case<R> when(Expression<Boolean> condition, Expression<? extends R> resul
8080
return this;
8181
}
8282

83-
@SuppressWarnings({ "unchecked" })
83+
@SuppressWarnings({"unchecked"})
8484
private void adjustJavaType(Expression<? extends R> exp) {
8585
if ( javaType == null ) {
8686
javaType = (Class<R>) exp.getJavaType();
8787
}
8888
}
8989

9090
public Expression<R> otherwise(R result) {
91-
return otherwise( buildLiteral(result) );
91+
return otherwise( buildLiteral( result ) );
9292
}
9393

9494
public Expression<R> otherwise(Expression<? extends R> result) {

hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/SimpleCaseExpression.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.hibernate.jpa.criteria.ParameterRegistry;
1717
import org.hibernate.jpa.criteria.Renderable;
1818
import org.hibernate.jpa.criteria.compile.RenderingContext;
19+
import org.hibernate.jpa.criteria.expression.function.CastFunction;
1920

2021
/**
2122
* Models what ANSI SQL terms a simple case statement. This is a <tt>CASE</tt> expression in the form<pre>
@@ -78,7 +79,7 @@ private LiteralExpression<R> buildLiteral(R result) {
7879
final Class<R> type = result != null
7980
? (Class<R>) result.getClass()
8081
: getJavaType();
81-
return new LiteralExpression<R>( criteriaBuilder(), type, result );
82+
return new CaseLiteralExpression<R>( criteriaBuilder(), type, result );
8283
}
8384

8485
public SimpleCase<C, R> when(C condition, Expression<? extends R> result) {

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/criteria/selectcase/SelectCaseTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
import javax.persistence.Id;
3030
import javax.persistence.criteria.CriteriaBuilder;
3131
import javax.persistence.criteria.CriteriaQuery;
32+
import javax.persistence.criteria.Expression;
3233
import javax.persistence.criteria.Predicate;
3334
import javax.persistence.criteria.Root;
3435
import java.util.List;
3536

37+
import org.hibernate.jpa.criteria.expression.ConcatExpression;
38+
import org.hibernate.jpa.criteria.expression.ExpressionImpl;
39+
import org.hibernate.jpa.criteria.expression.function.AbsFunction;
3640
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
3741

3842
import org.junit.Test;

0 commit comments

Comments
 (0)