From 6736d8334e22a9bc2981a32f8229cb463709ebcc Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 17 Oct 2023 15:27:57 +0200 Subject: [PATCH] Add conversion capabilities to the entity instantiator. Closes #1646 --- ...ctJdbcAggregateTemplateIntegrationTests.java | 17 +++++++++++++++++ ...dbcAggregateTemplateIntegrationTests-db2.sql | 14 ++++++++++++++ ...JdbcAggregateTemplateIntegrationTests-h2.sql | 11 +++++++++++ ...bcAggregateTemplateIntegrationTests-hsql.sql | 11 +++++++++++ ...ggregateTemplateIntegrationTests-mariadb.sql | 11 +++++++++++ ...cAggregateTemplateIntegrationTests-mssql.sql | 14 ++++++++++++++ ...cAggregateTemplateIntegrationTests-mysql.sql | 11 +++++++++++ ...AggregateTemplateIntegrationTests-oracle.sql | 14 ++++++++++++++ ...gregateTemplateIntegrationTests-postgres.sql | 14 ++++++++++++++ .../conversion/MappingRelationalConverter.java | 2 +- 10 files changed, 118 insertions(+), 1 deletion(-) 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 da8a8a78553..e20b0a9858c 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 @@ -1268,6 +1268,16 @@ void multipleCollectionsWithEmptyList() { assertThat(reloaded.mapElements.get("delta")).isEqualTo(new MapElement("four")); } + @Test // GH-1646 + void recordOfSet() { + + Author tolkien = template.save(new Author(null, Set.of(new Book("Lord of the Rings")))); + + Iterable authors = template.findAll(Author.class); + + assertThat(authors).containsExactly(tolkien); + } + private void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate, Function toConcreteNumber) { saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0); @@ -2079,6 +2089,13 @@ record SetElement(String name) { record MapElement(String name) { } + record Author(@Id Long id, Set books) { + } + + record Book(String name) { + + } + @Configuration @Import(TestConfiguration.class) static class Config { diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-db2.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-db2.sql index 2abc57c2794..e03527af815 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-db2.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-db2.sql @@ -47,6 +47,9 @@ DROP TABLE MAP_ELEMENT; DROP TABLE LIST_ELEMENT; DROP TABLE SET_ELEMENT; +DROP TABLE BOOK; +DROP TABLE AUTHOR; + CREATE TABLE LEGO_SET ( "id1" BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY, @@ -405,3 +408,14 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +CREATE TABLE AUTHOR +( + ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR BIGINT, + NAME VARCHAR(100) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-h2.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-h2.sql index 1b1da1a1ecf..9f255999763 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-h2.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-h2.sql @@ -367,3 +367,14 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +CREATE TABLE AUTHOR +( + ID SERIAL PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR BIGINT, + NAME VARCHAR(100) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-hsql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-hsql.sql index b74e716ebd1..80b2f8d10fa 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-hsql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-hsql.sql @@ -368,3 +368,14 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +CREATE TABLE AUTHOR +( + ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR BIGINT, + NAME VARCHAR(100) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mariadb.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mariadb.sql index 29f0140d865..677bd0d1363 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mariadb.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mariadb.sql @@ -341,3 +341,14 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +CREATE TABLE AUTHOR +( + ID BIGINT AUTO_INCREMENT PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR BIGINT, + NAME VARCHAR(100) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mssql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mssql.sql index cacd54ed6c3..bfb37923f2f 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mssql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mssql.sql @@ -379,3 +379,17 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +DROP TABLE BOOK; +DROP TABLE AUTHOR; + +CREATE TABLE AUTHOR +( + ID BIGINT IDENTITY PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR BIGINT, + NAME VARCHAR(100) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mysql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mysql.sql index 3efb07a000e..5d65e73004c 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mysql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-mysql.sql @@ -346,3 +346,14 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +CREATE TABLE AUTHOR +( + ID BIGINT AUTO_INCREMENT PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR BIGINT, + NAME VARCHAR(100) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-oracle.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-oracle.sql index c4255de27e2..0581608a033 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-oracle.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-oracle.sql @@ -37,6 +37,9 @@ DROP TABLE MAP_ELEMENT CASCADE CONSTRAINTS PURGE; DROP TABLE LIST_ELEMENT CASCADE CONSTRAINTS PURGE; DROP TABLE SET_ELEMENT CASCADE CONSTRAINTS PURGE; +DROP TABLE BOOK CASCADE CONSTRAINTS PURGE; +DROP TABLE AUTHOR CASCADE CONSTRAINTS PURGE; + CREATE TABLE LEGO_SET ( "id1" NUMBER GENERATED by default on null as IDENTITY PRIMARY KEY, @@ -386,3 +389,14 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +CREATE TABLE AUTHOR +( + ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR NUMBER, + NAME VARCHAR(100) +); diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-postgres.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-postgres.sql index e401412e59d..df4fe5a7c82 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-postgres.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/JdbcAggregateTemplateIntegrationTests-postgres.sql @@ -40,6 +40,9 @@ DROP TABLE MAP_ELEMENT; DROP TABLE LIST_ELEMENT; DROP TABLE SET_ELEMENT; +DROP TABLE BOOK; +DROP TABLE AUTHOR; + CREATE TABLE LEGO_SET ( "id1" SERIAL PRIMARY KEY, @@ -408,3 +411,14 @@ CREATE TABLE MAP_ELEMENT MULTIPLE_COLLECTIONS_KEY VARCHAR(10), NAME VARCHAR(100) ); + +CREATE TABLE AUTHOR +( + ID SERIAL PRIMARY KEY +); + +CREATE TABLE BOOK +( + AUTHOR BIGINT, + NAME VARCHAR(100) +); diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java index 8477103ac4f..94f2df11679 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java @@ -504,7 +504,7 @@ public RelationalPropertyValueProvider withContext(ConversionContext context) { entity, contextualizing, context.getPath().getCurrentObject()); return new ConverterAwareSpELExpressionParameterValueProvider(context, evaluator, getConversionService(), - parameterProvider); + new ConvertingParameterValueProvider<>( parameterProvider::getParameterValue)); } private S populateProperties(ConversionContext context, RelationalPersistentEntity entity,