Browse Source

Avoid double-conversion of values considered simple.

MappingJdbcConverter previously tried to create a JdbcValue for simple values via the ConversionService, only to drop the conversion result if the conversino did not result in a JdbcValue eventually. In that case it triggered an additional (same) conversion to then handle the wrapping of the conversion result into a JdbcValue manually.

This commit alters the flow so that it only triggers the conversion once and manually applies the JdbcValue wrapping only if the result of the original conversion is not a JdbcValue, yet.

Original pull request #1830
pull/1838/head
Oliver Drotbohm 1 year ago committed by Jens Schauder
parent
commit
112c084b29
No known key found for this signature in database
GPG Key ID: 74F6C554AE971567
  1. 25
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java

25
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java

@ -243,15 +243,14 @@ public class MappingJdbcConverter extends MappingRelationalConverter implements @@ -243,15 +243,14 @@ public class MappingJdbcConverter extends MappingRelationalConverter implements
@Override
public JdbcValue writeJdbcValue(@Nullable Object value, TypeInformation<?> columnType, SQLType sqlType) {
JdbcValue jdbcValue = tryToConvertToJdbcValue(value);
if (jdbcValue != null) {
return jdbcValue;
}
TypeInformation<?> targetType = canWriteAsJdbcValue(value) ? TypeInformation.of(JdbcValue.class) : columnType;
Object convertedValue = writeValue(value, targetType);
Object convertedValue = writeValue(value, columnType);
if (convertedValue instanceof JdbcValue result) {
return result;
}
if (convertedValue == null || !convertedValue.getClass().isArray()) {
return JdbcValue.of(convertedValue, sqlType);
}
@ -269,20 +268,6 @@ public class MappingJdbcConverter extends MappingRelationalConverter implements @@ -269,20 +268,6 @@ public class MappingJdbcConverter extends MappingRelationalConverter implements
return JdbcValue.of(convertedValue, JDBCType.BINARY);
}
@Nullable
private JdbcValue tryToConvertToJdbcValue(@Nullable Object value) {
if (canWriteAsJdbcValue(value)) {
Object converted = writeValue(value, TypeInformation.of(JdbcValue.class));
if (converted instanceof JdbcValue) {
return (JdbcValue) converted;
}
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public <R> R readAndResolve(TypeInformation<R> type, RowDocument source, Identifier identifier) {

Loading…
Cancel
Save