diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisContext.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisContext.java index 61bc84e5..0d7f50d7 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisContext.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisContext.java @@ -104,7 +104,12 @@ public class MyBatisContext { */ @Nullable public Object get(String key) { - return additionalValues.get(key); - } + Object value = null; + if (identifier != null) { + value = identifier.toMap().get(key); + } + + return value == null ? additionalValues.get(key) : value; + } } diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java index ea918b24..3902ce3a 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java @@ -153,8 +153,7 @@ public class MyBatisDataAccessStrategy implements DataAccessStrategy { @Override public Object insert(T instance, Class domainType, Identifier identifier) { - MyBatisContext myBatisContext = new MyBatisContext(null, instance, domainType, - convertToParameterMap(identifier.toMap())); + MyBatisContext myBatisContext = new MyBatisContext(identifier, instance, domainType); sqlSession().insert(namespace(domainType) + ".insert", myBatisContext); return myBatisContext.getId(); 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 56c05c1e..691f9cd8 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 @@ -22,8 +22,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Map; import org.assertj.core.api.Assertions; +import org.assertj.core.api.SoftAssertions; import org.junit.Test; import org.springframework.data.relational.core.sql.IdentifierProcessing; import org.springframework.data.relational.core.sql.SqlIdentifier; @@ -111,4 +113,23 @@ public class IdentifierUnitTests { assertThat(one).isEqualTo(two); assertThat(one).isNotEqualTo(three); } + + @Test // DATAJDBC-542 + public void identifierPartsCanBeAccessedByString() { + + Map idParts = new HashMap<>(); + idParts.put(unquoted("aName"), "one"); + idParts.put(quoted("Other"), "two"); + + Identifier id = Identifier.from(idParts); + + Map map = id.toMap(); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(map.get("aName")).describedAs("aName").isEqualTo("one"); + softly.assertThat(map.get("Other")).describedAs("Other").isEqualTo("two"); + softly.assertThat(map.get("other")).describedAs("other").isNull(); + softly.assertThat(map.get("OTHER")).describedAs("OTHER").isNull(); + }); + } } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/mybatis/MyBatisContextUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/mybatis/MyBatisContextUnitTests.java new file mode 100644 index 00000000..808f3bda --- /dev/null +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/mybatis/MyBatisContextUnitTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jdbc.mybatis; + +import java.util.HashMap; +import java.util.Map; + +import org.assertj.core.api.SoftAssertions; +import org.junit.Test; +import org.springframework.data.jdbc.core.convert.Identifier; +import org.springframework.data.relational.core.sql.SqlIdentifier; + +public class MyBatisContextUnitTests { + + @Test // DATAJDBC-542 + public void testGetReturnsValuesFromIdentifier() { + + Map map = new HashMap<>(); + map.put(SqlIdentifier.quoted("one"), "oneValue"); + map.put(SqlIdentifier.unquoted("two"), "twoValue"); + map.put(SqlIdentifier.quoted("Three"), "threeValue"); + map.put(SqlIdentifier.unquoted("Four"), "fourValue"); + + MyBatisContext context = new MyBatisContext(Identifier.from(map), null, null); + + SoftAssertions.assertSoftly(softly -> { + + softly.assertThat(context.get("one")).isEqualTo("oneValue"); + softly.assertThat(context.get("two")).isEqualTo("twoValue"); + softly.assertThat(context.get("Three")).isEqualTo("threeValue"); + softly.assertThat(context.get("Four")).isEqualTo("fourValue"); + softly.assertThat(context.get("four")).isNull(); + softly.assertThat(context.get("five")).isNull(); + }); + } + +} diff --git a/spring-data-relational/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java b/spring-data-relational/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java index 282a3d3e..a92d1124 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java +++ b/spring-data-relational/src/main/java/org/springframework/data/jdbc/core/convert/Identifier.java @@ -27,6 +27,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import org.springframework.data.relational.core.sql.SqlIdentifier; import org.springframework.util.Assert; @@ -144,7 +145,7 @@ public final class Identifier { */ public Map toMap() { - Map result = new LinkedHashMap<>(); + Map result = new StringKeyedLinkedHashMap<>(); forEach((name, value, type) -> result.put(name, value)); return result; } @@ -212,4 +213,21 @@ public final class Identifier { */ void accept(SqlIdentifier name, Object value, Class targetType); } + + private static class StringKeyedLinkedHashMap extends LinkedHashMap { + + @Override + public V get(Object key) { + + if (key instanceof String) { + + for (SqlIdentifier sqlIdentifier : keySet()) { + if (sqlIdentifier.getReference().equals(key)) { + return super.get(sqlIdentifier); + } + } + } + return super.get(key); + } + } }