From dfd87a9ca8434114e4a0ea3bf689e73e9a4ee5c4 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 16 Apr 2024 16:18:01 +0200 Subject: [PATCH 1/2] 1692-collection-in-embedded - Prepare branch --- pom.xml | 2 +- spring-data-jdbc-distribution/pom.xml | 2 +- spring-data-jdbc/pom.xml | 4 ++-- spring-data-r2dbc/pom.xml | 4 ++-- spring-data-relational/pom.xml | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 02461b8e40..a0f8c4e752 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-relational-parent - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT pom Spring Data Relational Parent diff --git a/spring-data-jdbc-distribution/pom.xml b/spring-data-jdbc-distribution/pom.xml index 8d987fb028..51c65d5073 100644 --- a/spring-data-jdbc-distribution/pom.xml +++ b/spring-data-jdbc-distribution/pom.xml @@ -14,7 +14,7 @@ org.springframework.data spring-data-relational-parent - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT ../pom.xml diff --git a/spring-data-jdbc/pom.xml b/spring-data-jdbc/pom.xml index fddbaab696..c57532d107 100644 --- a/spring-data-jdbc/pom.xml +++ b/spring-data-jdbc/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-jdbc - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT Spring Data JDBC Spring Data module for JDBC repositories. @@ -15,7 +15,7 @@ org.springframework.data spring-data-relational-parent - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT diff --git a/spring-data-r2dbc/pom.xml b/spring-data-r2dbc/pom.xml index c834dc4cab..7d6355aa00 100644 --- a/spring-data-r2dbc/pom.xml +++ b/spring-data-r2dbc/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-r2dbc - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT Spring Data R2DBC Spring Data module for R2DBC @@ -15,7 +15,7 @@ org.springframework.data spring-data-relational-parent - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT diff --git a/spring-data-relational/pom.xml b/spring-data-relational/pom.xml index 671e71d242..9a024c5631 100644 --- a/spring-data-relational/pom.xml +++ b/spring-data-relational/pom.xml @@ -6,7 +6,7 @@ 4.0.0 spring-data-relational - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT Spring Data Relational Spring Data Relational support @@ -14,7 +14,7 @@ org.springframework.data spring-data-relational-parent - 3.3.0-SNAPSHOT + 3.3.0-1692-collection-in-embedded-SNAPSHOT From 4e16b05e288031d75bf74f879a5feb606f83287f Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 16 Apr 2024 16:54:48 +0200 Subject: [PATCH 2/2] Fix loading of 2nd level collections. Construction of the backreference assumed that the table holding the parent of the foreign key is the actual parent property. This is now corrected by using the correct API to identify the ancestor which holds the id. Closes: #1692 --- .../core/convert/MappingJdbcConverter.java | 18 +++++++++++------- ...EmbeddedWithCollectionIntegrationTests.java | 11 ++++++----- ...eddedWithCollectionIntegrationTests-db2.sql | 4 ++-- ...beddedWithCollectionIntegrationTests-h2.sql | 4 ++-- ...ddedWithCollectionIntegrationTests-hsql.sql | 4 ++-- ...dWithCollectionIntegrationTests-mariadb.sql | 15 +++++++++++++-- ...dedWithCollectionIntegrationTests-mssql.sql | 15 +++++++++++++-- ...dedWithCollectionIntegrationTests-mysql.sql | 15 +++++++++++++-- ...edWithCollectionIntegrationTests-oracle.sql | 14 +++++++------- ...WithCollectionIntegrationTests-postgres.sql | 4 ++-- 10 files changed, 71 insertions(+), 33 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 a4cb8e08ad..746fd3664e 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 @@ -26,7 +26,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.springframework.context.ApplicationContextAware; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.converter.Converter; @@ -45,6 +44,7 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; +import org.springframework.data.relational.core.sql.SqlIdentifier; import org.springframework.data.relational.domain.RowDocument; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; @@ -366,15 +366,19 @@ public T getPropertyValue(RelationalPersistentProperty property) { if (property.isCollectionLike() || property.isMap()) { Identifier identifierToUse = this.identifier; + AggregatePath idDefiningParentPath = aggregatePath.getIdDefiningParentPath(); + + // note that the idDefiningParentPath might not itself have an id property, but have a combination of back + // references and possibly keys, that form an id + if (idDefiningParentPath.hasIdProperty()) { - if (property.getOwner().hasIdProperty()) { + Class idType = idDefiningParentPath.getRequiredIdProperty().getActualType(); + SqlIdentifier parentId = idDefiningParentPath.getTableInfo().idColumnName(); + Object idValue = this.identifier.get(parentId); - Object id = this.identifier.get(property.getOwner().getRequiredIdProperty().getColumnName()); + Assert.state(idValue != null, "idValue must not be null at this point"); - if (id != null) { - identifierToUse = Identifier.of(aggregatePath.getTableInfo().reverseColumnInfo().name(), id, - Object.class); - } + identifierToUse = Identifier.of(aggregatePath.getTableInfo().reverseColumnInfo().name(), idValue, idType); } Iterable allByPath = relationResolver.findAllByPath(identifierToUse, diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests.java index b55b609446..f04a574215 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests.java @@ -69,13 +69,13 @@ public void savesAnEntity() throws SQLException { DummyEntity entity = repository.save(createDummyEntity()); - assertThat(countRowsInTable("dummy_entity", entity.getId())).isEqualTo(1); - assertThat(countRowsInTable("dummy_entity2", entity.getId())).isEqualTo(2); + assertThat(countRowsInTable("dummy_entity", entity.getId(), "ID")).isEqualTo(1); + assertThat(countRowsInTable("dummy_entity2", entity.getId(), "DUMMY_ID")).isEqualTo(2); } - private int countRowsInTable(String name, long idValue) { + private int countRowsInTable(String name, long idValue, String idColumnName) { - SqlIdentifier id = SqlIdentifier.quoted("ID"); + SqlIdentifier id = SqlIdentifier.quoted(idColumnName); String whereClause = id.toSql(dialect.getIdentifierProcessing()) + " = " + idValue; return JdbcTestUtils.countRowsInTableWhere(template.getJdbcOperations(), name, whereClause); @@ -273,7 +273,8 @@ public void setEmbeddable(Embeddable embeddable) { } private static class Embeddable { - @MappedCollection(idColumn = "ID", keyColumn = "ORDER_KEY") List list = new ArrayList<>(); + @MappedCollection(idColumn = "DUMMY_ID", keyColumn = "ORDER_KEY") + List list = new ArrayList<>(); String test; diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-db2.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-db2.sql index 3d3b73d803..c8fdf5b9a5 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-db2.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-db2.sql @@ -9,8 +9,8 @@ CREATE TABLE dummy_entity ); CREATE TABLE dummy_entity2 ( - id BIGINT NOT NULL, + dummy_id BIGINT NOT NULL, ORDER_KEY BIGINT NOT NULL, TEST VARCHAR(100), - PRIMARY KEY (id, ORDER_KEY) + PRIMARY KEY (dummy_id, ORDER_KEY) ) diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-h2.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-h2.sql index 0a8a90711c..fe9d025fd7 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-h2.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-h2.sql @@ -6,8 +6,8 @@ CREATE TABLE dummy_entity ); CREATE TABLE dummy_entity2 ( - id BIGINT, + dummy_id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), - PRIMARY KEY (id, ORDER_KEY) + PRIMARY KEY (dummy_id, ORDER_KEY) ) diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-hsql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-hsql.sql index 0a8a90711c..1b885a7867 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-hsql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-hsql.sql @@ -6,8 +6,8 @@ CREATE TABLE dummy_entity ); CREATE TABLE dummy_entity2 ( - id BIGINT, + dummy_id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), - PRIMARY KEY (id, ORDER_KEY) + PRIMARY KEY (dummy_id, ORDER_KEY) ) diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mariadb.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mariadb.sql index d4fc5f8f31..c08506512e 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mariadb.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mariadb.sql @@ -1,2 +1,13 @@ -CREATE TABLE dummy_entity (id BIGINT AUTO_INCREMENT PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100)); -CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), PRIMARY KEY(id, ORDER_KEY)); +CREATE TABLE dummy_entity +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + TEST VARCHAR(100), + PREFIX_TEST VARCHAR(100) +); +CREATE TABLE dummy_entity2 +( + dummy_id BIGINT, + ORDER_KEY BIGINT, + TEST VARCHAR(100), + PRIMARY KEY (dummy_id, ORDER_KEY) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mssql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mssql.sql index 327e50beb1..2cfda4b6c3 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mssql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mssql.sql @@ -1,4 +1,15 @@ DROP TABLE IF EXISTS dummy_entity; -CREATE TABLE dummy_entity (id BIGINT IDENTITY PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100)); +CREATE TABLE dummy_entity +( + id BIGINT IDENTITY PRIMARY KEY, + TEST VARCHAR(100), + PREFIX_TEST VARCHAR(100) +); DROP TABLE IF EXISTS dummy_entity2; -CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), CONSTRAINT dummym_entity2_pk PRIMARY KEY(id, ORDER_KEY)); \ No newline at end of file +CREATE TABLE dummy_entity2 +( + dummy_id BIGINT, + ORDER_KEY BIGINT, + TEST VARCHAR(100), + CONSTRAINT dummym_entity2_pk PRIMARY KEY (dummy_id, ORDER_KEY) +); \ No newline at end of file diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mysql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mysql.sql index d4fc5f8f31..c08506512e 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mysql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-mysql.sql @@ -1,2 +1,13 @@ -CREATE TABLE dummy_entity (id BIGINT AUTO_INCREMENT PRIMARY KEY, TEST VARCHAR(100), PREFIX_TEST VARCHAR(100)); -CREATE TABLE dummy_entity2 (id BIGINT, ORDER_KEY BIGINT, TEST VARCHAR(100), PRIMARY KEY(id, ORDER_KEY)); +CREATE TABLE dummy_entity +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + TEST VARCHAR(100), + PREFIX_TEST VARCHAR(100) +); +CREATE TABLE dummy_entity2 +( + dummy_id BIGINT, + ORDER_KEY BIGINT, + TEST VARCHAR(100), + PRIMARY KEY (dummy_id, ORDER_KEY) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-oracle.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-oracle.sql index 66f369fc10..2d538df140 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-oracle.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-oracle.sql @@ -3,15 +3,15 @@ DROP TABLE DUMMY_ENTITY CASCADE CONSTRAINTS PURGE; CREATE TABLE DUMMY_ENTITY ( - ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY, - TEST VARCHAR2(100), - PREFIX_TEST VARCHAR2(100) + ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY, + TEST VARCHAR2(100), + PREFIX_TEST VARCHAR2(100) ); CREATE TABLE DUMMY_ENTITY2 ( - ID NUMBER, - ORDER_KEY NUMBER, - TEST VARCHAR2(100), - PRIMARY KEY (ID, ORDER_KEY) + DUMMY_ID NUMBER, + ORDER_KEY NUMBER, + TEST VARCHAR2(100), + PRIMARY KEY (DUMMY_ID, ORDER_KEY) ) diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-postgres.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-postgres.sql index 4b49f1ef89..e16ee36311 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-postgres.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryEmbeddedWithCollectionIntegrationTests-postgres.sql @@ -8,8 +8,8 @@ CREATE TABLE dummy_entity DROP TABLE dummy_entity2; CREATE TABLE dummy_entity2 ( - "ID" BIGINT, + "DUMMY_ID" BIGINT, "ORDER_KEY" BIGINT, TEST VARCHAR(100), - PRIMARY KEY ("ID", "ORDER_KEY") + PRIMARY KEY ("DUMMY_ID", "ORDER_KEY") );