From a900ee8928111f819b999000d14bc850386b6fee Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Tue, 22 Dec 2009 06:12:34 +0000 Subject: [PATCH] HHH-4698 - Better handling of JPA criteria expressions git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18312 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../ejb/criteria/ValueConverter.java | 28 ++++++++++++++----- .../predicate/ComparisonPredicate.java | 2 +- .../org/hibernate/ejb/metamodel/Info.java | 2 +- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/ValueConverter.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ValueConverter.java index 898d5fb7a3..c74967e382 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/ValueConverter.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ValueConverter.java @@ -39,6 +39,16 @@ public class ValueConverter { public T apply(Object value); } + public static boolean isNumeric(Class type) { + return Number.class.isAssignableFrom( type ) + || Byte.TYPE.equals( type ) + || Short.TYPE.equals( type ) + || Integer.TYPE.equals( type ) + || Long.TYPE.isAssignableFrom( type ) + || Float.TYPE.equals( type ) + || Double.TYPE.isAssignableFrom( type ); + } + public static class ByteConversion implements Conversion { public static final ByteConversion INSTANCE = new ByteConversion(); @SuppressWarnings({ "UnnecessaryBoxing" }) @@ -185,10 +195,14 @@ public class ValueConverter { private static IllegalArgumentException unknownConversion(Object value, Class type) { return new IllegalArgumentException( - "Unaware how to convert value [" + value + "] to requested type [" + type.getName() + "]" + "Unaware how to convert value [" + value + " : " + typeName( value ) + "] to requested type [" + type.getName() + "]" ); } + private static String typeName(Object value) { + return value == null ? "???" : value.getClass().getName(); + } + /** * Convert the given value into the specified target type. * @@ -226,22 +240,22 @@ public class ValueConverter { if ( String.class.equals( targetType ) ) { return (Conversion) StringConversion.INSTANCE; } - if ( Byte.class.equals( targetType ) ) { + if ( Byte.class.equals( targetType ) || Byte.TYPE.equals( targetType ) ) { return (Conversion) ByteConversion.INSTANCE; } - if ( Short.class.equals( targetType ) ) { + if ( Short.class.equals( targetType ) || Short.TYPE.equals( targetType ) ) { return (Conversion) ShortConversion.INSTANCE; } - if ( Integer.class.equals( targetType ) ) { + if ( Integer.class.equals( targetType ) || Integer.TYPE.equals( targetType ) ) { return (Conversion) IntegerConversion.INSTANCE; } - if ( Long.class.equals( targetType ) ) { + if ( Long.class.equals( targetType ) || Long.TYPE.equals( targetType ) ) { return (Conversion) LongConversion.INSTANCE; } - if ( Float.class.equals( targetType ) ) { + if ( Float.class.equals( targetType ) || Float.TYPE.equals( targetType ) ) { return (Conversion) FloatConversion.INSTANCE; } - if ( Double.class.equals( targetType ) ) { + if ( Double.class.equals( targetType ) || Double.TYPE.equals( targetType ) ) { return (Conversion) DoubleConversion.INSTANCE; } if ( BigInteger.class.equals( targetType ) ) { diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java index cfd688a54e..305f3820c0 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java @@ -63,7 +63,7 @@ public class ComparisonPredicate extends AbstractSimplePredicate implements Bina super( criteriaBuilder ); this.comparisonOperator = comparisonOperator; this.leftHandSide = leftHandSide; - if ( Number.class.isAssignableFrom( leftHandSide.getJavaType() ) ) { + if ( ValueConverter.isNumeric( leftHandSide.getJavaType() ) ) { this.rightHandSide = new LiteralExpression( criteriaBuilder, ValueConverter.convert( rightHandSide, (Class) leftHandSide.getJavaType() ) diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Info.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Info.java index 019093f808..0566b1717b 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Info.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Info.java @@ -113,7 +113,7 @@ public class Info implements java.io.Serializable { zip = v; } - @OneToOne(mappedBy = "info") @JoinTable + @OneToOne(mappedBy = "info") @JoinTable( name = "INFO_SPOUSE_TABLE" ) public Spouse getSpouse() { return spouse; }