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 Christian Beikov
parent ea5d952ec0
commit 3c88dfd509
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();
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();

View File

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

View File

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

View File

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

View File

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

View File

@ -15,13 +15,17 @@ import org.hibernate.type.descriptor.java.spi.UnknownBasicJavaType;
public class JavaTypeHelper {
protected static <T extends JavaType<?>> HibernateException unknownUnwrap(Class<?> sourceType, Class<?> targetType, T jtd) {
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) {
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(
String.format(
Locale.ROOT,
"Cannot coerce value `%s` [%s] as Long",
"Cannot coerce value '%s' [%s] to Long",
value,
value.getClass().getName()
)

View File

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

View File

@ -79,6 +79,14 @@ public class StringJavaType extends AbstractClassJavaType<String> {
if ( Clob.class.isAssignableFrom( type ) ) {
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 );
}
@ -99,6 +107,12 @@ public class StringJavaType extends AbstractClassJavaType<String> {
if (value instanceof Clob) {
return DataHelper.extractString( (Clob) value );
}
if (value instanceof Integer) {
return value.toString();
}
if (value instanceof Long) {
return value.toString();
}
throw unknownWrap( value.getClass() );
}