improve some error messages

also, tell StringType know how to convert ints and longs
This commit is contained in:
Gavin 2023-05-19 13:44:14 +02:00 committed by Gavin King
parent 8ddbb033cd
commit 689414e347
9 changed files with 34 additions and 9 deletions

View File

@ -173,7 +173,14 @@ public class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T>, Jav
final JpaCompliance jpaCompliance = session.getFactory().getSessionFactoryOptions().getJpaCompliance(); final JpaCompliance jpaCompliance = session.getFactory().getSessionFactoryOptions().getJpaCompliance();
if ( ! jpaCompliance.isLoadByIdComplianceEnabled() ) { if ( ! jpaCompliance.isLoadByIdComplianceEnabled() ) {
id = entityPersister.getIdentifierMapping().getJavaType().coerce( id, this ); try {
id = entityPersister.getIdentifierMapping().getJavaType().coerce( id, this );
}
catch ( Exception e ) {
throw new IllegalArgumentException( "Argument '" + id
+ "' could not be converted to the identifier type of entity '" + entityPersister.getEntityName() + "'"
+ " [" + e.getMessage() + "]", e );
}
} }
String entityName = entityPersister.getEntityName(); String entityName = entityPersister.getEntityName();

View File

@ -169,7 +169,7 @@ public class ByteJavaType extends AbstractClassJavaType<Byte>
throw new CoercionException( throw new CoercionException(
String.format( String.format(
Locale.ROOT, Locale.ROOT,
"Cannot coerce value `%s` [%s] as Byte", "Cannot coerce value '%s' [%s] to Byte",
value, value,
value.getClass().getName() value.getClass().getName()
) )

View File

@ -201,7 +201,7 @@ public class DoubleJavaType extends AbstractClassJavaType<Double> implements
throw new CoercionException( throw new CoercionException(
String.format( String.format(
Locale.ROOT, Locale.ROOT,
"Cannot coerce value `%s` [%s] as Double", "Cannot coerce value '%s' [%s] to Double",
value, value,
value.getClass().getName() value.getClass().getName()
) )

View File

@ -196,7 +196,7 @@ public class FloatJavaType extends AbstractClassJavaType<Float> implements Primi
throw new CoercionException( throw new CoercionException(
String.format( String.format(
Locale.ROOT, Locale.ROOT,
"Cannot coerce value `%s` [%s] as Float", "Cannot coerce value '%s' [%s] to Float",
value, value,
value.getClass().getName() value.getClass().getName()
) )

View File

@ -187,7 +187,7 @@ public class IntegerJavaType extends AbstractClassJavaType<Integer>
throw new CoercionException( throw new CoercionException(
String.format( String.format(
Locale.ROOT, Locale.ROOT,
"Cannot coerce value `%s` [%s] as Integer", "Cannot coerce value '%s' [%s] to Integer",
value, value,
value.getClass().getName() value.getClass().getName()
) )

View File

@ -15,13 +15,17 @@ import org.hibernate.type.descriptor.java.spi.UnknownBasicJavaType;
public class JavaTypeHelper { public class JavaTypeHelper {
protected static <T extends JavaType<?>> HibernateException unknownUnwrap(Class<?> sourceType, Class<?> targetType, T jtd) { protected static <T extends JavaType<?>> HibernateException unknownUnwrap(Class<?> sourceType, Class<?> targetType, T jtd) {
throw new HibernateException( throw new HibernateException(
"Unknown unwrap conversion requested: " + sourceType.getName() + " to " + targetType.getName() + " : `" + jtd.getClass().getName() + "` (" + jtd.getJavaTypeClass().getName() + ")" "Could not convert '" + sourceType.getName()
+ "' to '" + targetType.getName()
+ "' using '" + jtd.getClass().getName() + "' to unwrap"
); );
} }
protected static <T extends JavaType<?>> HibernateException unknownWrap(Class<?> valueType, Class<?> sourceType, T jtd) { protected static <T extends JavaType<?>> HibernateException unknownWrap(Class<?> valueType, Class<?> sourceType, T jtd) {
throw new HibernateException( throw new HibernateException(
"Unknown wrap conversion requested: " + valueType.getName() + " to " + sourceType.getName() + " : `" + jtd.getClass().getName() + "` (" + jtd.getJavaTypeClass().getName() + ")" "Could not convert '" + valueType.getName()
+ "' to '" + sourceType.getName()
+ "' using '" + jtd.getClass().getName() + "' to wrap"
); );
} }

View File

@ -154,7 +154,7 @@ public class LongJavaType extends AbstractClassJavaType<Long>
throw new CoercionException( throw new CoercionException(
String.format( String.format(
Locale.ROOT, Locale.ROOT,
"Cannot coerce value `%s` [%s] as Long", "Cannot coerce value '%s' [%s] to Long",
value, value,
value.getClass().getName() value.getClass().getName()
) )

View File

@ -177,7 +177,7 @@ public class ShortJavaType extends AbstractClassJavaType<Short>
throw new CoercionException( throw new CoercionException(
String.format( String.format(
Locale.ROOT, Locale.ROOT,
"Cannot coerce value `%s` [%s] as Short", "Cannot coerce value '%s' [%s] to Short",
value, value,
value.getClass().getName() value.getClass().getName()
) )

View File

@ -79,6 +79,14 @@ public class StringJavaType extends AbstractClassJavaType<String> {
if ( Clob.class.isAssignableFrom( type ) ) { if ( Clob.class.isAssignableFrom( type ) ) {
return (X) options.getLobCreator().createClob( value ); return (X) options.getLobCreator().createClob( value );
} }
if ( Integer.class.isAssignableFrom( type ) ) {
Integer parsed = Integer.parseInt( value );
return (X) parsed;
}
if ( Long.class.isAssignableFrom( type ) ) {
Long parsed = Long.parseLong( value );
return (X) parsed;
}
throw unknownUnwrap( type ); throw unknownUnwrap( type );
} }
@ -99,6 +107,12 @@ public class StringJavaType extends AbstractClassJavaType<String> {
if (value instanceof Clob) { if (value instanceof Clob) {
return DataHelper.extractString( (Clob) value ); return DataHelper.extractString( (Clob) value );
} }
if (value instanceof Integer) {
return value.toString();
}
if (value instanceof Long) {
return value.toString();
}
throw unknownWrap( value.getClass() ); throw unknownWrap( value.getClass() );
} }