diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java index 7a3dcc4ee..d98676cd7 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java @@ -24,7 +24,6 @@ import java.util.Map; import java.util.Objects; import org.springframework.data.relational.core.sql.SqlIdentifier; -import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -60,15 +59,15 @@ public final class Identifier { /** * Creates an {@link Identifier} from {@code name}, {@code value}, and a {@link Class target type}. * - * @param name must not be {@literal null} or empty. - * @param value must not be null + * @param name must not be {@literal null}. + * @param value must not be {@literal null}. * @param targetType must not be {@literal null}. * @return the {@link Identifier} for {@code name}, {@code value}, and a {@link Class target type}. */ public static Identifier of(SqlIdentifier name, Object value, Class targetType) { - Assert.notNull(name, "Name must not be empty"); - Assert.notNull(value, "Value must not be empty"); + Assert.notNull(name, "Name must not be null"); + Assert.notNull(value, "Value must not be null"); Assert.notNull(targetType, "Target type must not be null"); return new Identifier(Collections.singletonList(new SingleIdentifierValue(name, value, targetType))); @@ -92,7 +91,8 @@ public final class Identifier { map.forEach((k, v) -> { - Assert.notNull(v, "The source map for identifier cannot contain null values"); + Assert.notNull(v, "The source map for identifier must not contain null values"); + values.add(new SingleIdentifierValue(k, v, ClassUtils.getUserClass(v))); }); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/IdentifierUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/IdentifierUnitTests.java index cfde059eb..dbdcc0edf 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/IdentifierUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/IdentifierUnitTests.java @@ -47,19 +47,28 @@ public class IdentifierUnitTests { } @Test // DATAJDBC-326 - public void parametersWithStringKeysUseObjectAsTypeForNull() { + public void typeIsCalculatedCorrectly() { HashMap parameters = new HashMap<>(); - Object value = new Object(); + Object objectValue = new Object(); + Object stringValue = "text"; + Object intValue = 23; + Object integerValue = 42; - parameters.put(unquoted("one"), value); + parameters.put(unquoted("one"), objectValue); + parameters.put(unquoted("two"), stringValue); + parameters.put(unquoted("three"), intValue); + parameters.put(unquoted("four"), integerValue); Identifier identifier = Identifier.from(parameters); assertThat(identifier.getParts()) // .extracting("name", "value", "targetType") // - .containsExactly( // - Assertions.tuple(unquoted("one"), value, Object.class) // + .containsExactlyInAnyOrder( // + Assertions.tuple(unquoted("one"), objectValue, Object.class), // + Assertions.tuple(unquoted("two"), stringValue, String.class), // + Assertions.tuple(unquoted("three"), intValue, Integer.class), // + Assertions.tuple(unquoted("four"), integerValue, Integer.class) // ); }