From 4b99c1b79da449a1811570b8d60910912467ccdb Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 27 Nov 2015 13:09:56 +0000 Subject: [PATCH] HHH-9343 - Case/when in criteria with string literal result fails type checking --- .../jpa/criteria/ValueHandlerFactory.java | 24 +++++++++++++++++++ .../expression/LiteralExpression.java | 3 +++ 2 files changed, 27 insertions(+) diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/ValueHandlerFactory.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/ValueHandlerFactory.java index 43af3a271f..24574805b9 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/ValueHandlerFactory.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/ValueHandlerFactory.java @@ -68,6 +68,10 @@ public class ValueHandlerFactory { || Double.TYPE.isInstance( value ); } + public static boolean isBoolean(Object value) { + return Boolean.class.isInstance( value ); + } + public static class ByteValueHandler extends BaseValueHandler implements Serializable { public static final ByteValueHandler INSTANCE = new ByteValueHandler(); @SuppressWarnings({ "UnnecessaryBoxing" }) @@ -230,6 +234,23 @@ public class ValueHandlerFactory { } } + public static class BooleanValueHandler extends BaseValueHandler implements Serializable { + public static final BooleanValueHandler INSTANCE = new BooleanValueHandler(); + + public Boolean convert(Object value) { + if ( value == null ) { + return null; + } + if ( Boolean.class.isInstance( value ) ) { + return (Boolean) value; + } + if ( String.class.isInstance( value ) ) { + return Boolean.getBoolean( (String) value ); + } + throw unknownConversion( value, Boolean.class ); + } + } + public static class StringValueHandler extends BaseValueHandler implements Serializable { public static final StringValueHandler INSTANCE = new StringValueHandler(); public String convert(Object value) { @@ -308,6 +329,9 @@ public class ValueHandlerFactory { if ( BigDecimal.class.equals( targetType ) ) { return (ValueHandler) BigDecimalValueHandler.INSTANCE; } + if ( Boolean.class.equals( targetType ) ) { + return (ValueHandler) BooleanValueHandler.INSTANCE; + } return null; } } diff --git a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/LiteralExpression.java b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/LiteralExpression.java index 73e31a1fc2..ac485b47c6 100644 --- a/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/LiteralExpression.java +++ b/hibernate-entitymanager/src/main/java/org/hibernate/jpa/criteria/expression/LiteralExpression.java @@ -49,6 +49,9 @@ public class LiteralExpression extends ExpressionImpl implements Serializa if ( ValueHandlerFactory.isNumeric( literal ) ) { return ValueHandlerFactory.determineAppropriateHandler( (Class) literal.getClass() ).render( literal ); } + else if ( ValueHandlerFactory.isBoolean( literal ) ) { + return ValueHandlerFactory.determineAppropriateHandler( (Class) literal.getClass() ).render( literal ); + } // else... final String parameterName = renderingContext.registerLiteralParameterBinding( getLiteral(), getJavaType() );