From 5344197ba689f1453e0e604e030311fa63f44456 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 12 Oct 2023 14:20:31 +0200 Subject: [PATCH] Fix identifier propagation for no-id list elements. Original pull request #1618 See #1554 --- .../convert/DefaultDataAccessStrategy.java | 26 +++++- ...JdbcAggregateTemplateIntegrationTests.java | 85 ++++++------------- 2 files changed, 47 insertions(+), 64 deletions(-) diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java index e1ad40cfff..6cd53f6b63 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java @@ -18,6 +18,7 @@ import static org.springframework.data.jdbc.core.convert.SqlGenerator.*; import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -29,6 +30,7 @@ import org.springframework.data.mapping.PersistentPropertyPath; import org.springframework.data.relational.core.conversion.IdValueSource; import org.springframework.data.relational.core.mapping.AggregatePath; +import org.springframework.data.relational.core.mapping.AggregatePath.TableInfo; import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; @@ -300,11 +302,27 @@ public Iterable findAllByPath(Identifier identifier, String findAllByProperty = sql(actualType) // .getFindAllByProperty(identifier, propertyPath); - RowMapper rowMapper = path.isMap() ? this.getMapEntityRowMapper(path, identifier) - : this.getEntityRowMapper(path, identifier); - SqlParameterSource parameterSource = sqlParametersFactory.forQueryByIdentifier(identifier); - return operations.query(findAllByProperty, parameterSource, (RowMapper) rowMapper); + return operations.query(findAllByProperty, parameterSource, new RowMapper<>() { + + @Override + public Object mapRow(ResultSet rs, int rowNum) throws SQLException { + + if (path.isMap()) { + return getMapEntityRowMapper(path, identifier).mapRow(rs, rowNum); + } + + // Add row number as key for paths that do not defile an identifier and that are contained in a collection. + Identifier identifierToUse = identifier; + if (!path.hasIdProperty() && path.isQualified()) { + + TableInfo tableInfo = path.getTableInfo(); + identifierToUse = identifierToUse.withPart(tableInfo.qualifierColumnInfo().name(), rowNum, Object.class); + } + + return getEntityRowMapper(path, identifierToUse).mapRow(rs, rowNum); + } + }); } @Override diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java index 99f505b87e..da8a8a7855 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java @@ -883,6 +883,9 @@ void saveAndLoadLongChainOfListsWithoutIds() { assertThat(saved.four).describedAs("Something went wrong during saving").isNotNull(); NoIdListChain4 reloaded = template.findById(saved.four, NoIdListChain4.class); + + assertThat(reloaded.chain3).hasSameSizeAs(saved.chain3); + assertThat(reloaded.chain3.get(0).chain2).hasSameSizeAs(saved.chain3.get(0).chain2); assertThat(reloaded).isEqualTo(saved); } @@ -1537,11 +1540,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [zeroValue='").append(zeroValue).append('\''); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [zeroValue='" + zeroValue + '\'' + ']'; + return sb; } } @@ -1566,12 +1566,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [oneValue='").append(oneValue).append('\''); - sb.append(", chain0=").append(chain0); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [oneValue='" + oneValue + '\'' + ", chain0=" + chain0 + ']'; + return sb; } } @@ -1596,12 +1592,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [twoValue='").append(twoValue).append('\''); - sb.append(", chain1=").append(chain1); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [twoValue='" + twoValue + '\'' + ", chain1=" + chain1 + ']'; + return sb; } } @@ -1626,12 +1618,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [threeValue='").append(threeValue).append('\''); - sb.append(", chain2=").append(chain2); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [threeValue='" + threeValue + '\'' + ", chain2=" + chain2 + ']'; + return sb; } } @@ -1658,13 +1646,9 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [four=").append(four); - sb.append(", fourValue='").append(fourValue).append('\''); - sb.append(", chain3=").append(chain3); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [four=" + four + ", fourValue='" + fourValue + '\'' + ", chain3=" + + chain3 + ']'; + return sb; } } @@ -1692,11 +1676,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [zeroValue='").append(zeroValue).append('\''); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [zeroValue='" + zeroValue + '\'' + ']'; + return sb; } } @@ -1721,12 +1702,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [oneValue='").append(oneValue).append('\''); - sb.append(", chain0=").append(chain0); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [oneValue='" + oneValue + '\'' + ", chain0=" + chain0 + ']'; + return sb; } } @@ -1751,12 +1728,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [twoValue='").append(twoValue).append('\''); - sb.append(", chain1=").append(chain1); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [twoValue='" + twoValue + '\'' + ", chain1=" + chain1 + ']'; + return sb; } } @@ -1781,12 +1754,8 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [threeValue='").append(threeValue).append('\''); - sb.append(", chain2=").append(chain2); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [threeValue='" + threeValue + '\'' + ", chain2=" + chain2 + ']'; + return sb; } } @@ -1813,13 +1782,9 @@ public int hashCode() { @Override public String toString() { - final StringBuffer sb = new StringBuffer(); - sb.append(getClass().getSimpleName()); - sb.append(" [four=").append(four); - sb.append(", fourValue='").append(fourValue).append('\''); - sb.append(", chain3=").append(chain3); - sb.append(']'); - return sb.toString(); + String sb = getClass().getSimpleName() + " [four=" + four + ", fourValue='" + fourValue + '\'' + ", chain3=" + + chain3 + ']'; + return sb; } }