From 59bcec754812c8c1bf921dc7a279832e1d7faa38 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 8 Jul 2024 14:27:58 +0200 Subject: [PATCH] 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. --- .../core/convert/MappingJdbcConverter.java | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java index 3810efd12b..258c1aefc4 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java @@ -243,15 +243,14 @@ private boolean canWriteAsJdbcValue(@Nullable Object value) { @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 JdbcValue writeJdbcValue(@Nullable Object value, TypeInformation colum 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 readAndResolve(TypeInformation type, RowDocument source, Identifier identifier) {