diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests.java index 8fcc7c132..6e740f525 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests.java @@ -15,26 +15,38 @@ */ package org.springframework.data.jdbc.repository; +import static java.util.Arrays.*; import static org.assertj.core.api.Assertions.*; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.Import; +import org.springframework.core.convert.converter.Converter; import org.springframework.data.annotation.Id; +import org.springframework.data.convert.CustomConversions; +import org.springframework.data.convert.ReadingConverter; +import org.springframework.data.convert.WritingConverter; +import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; import org.springframework.data.jdbc.core.mapping.AggregateReference; +import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes; import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; import org.springframework.data.jdbc.testing.DatabaseType; import org.springframework.data.jdbc.testing.EnabledOnDatabase; import org.springframework.data.jdbc.testing.IntegrationTest; import org.springframework.data.jdbc.testing.TestConfiguration; +import org.springframework.data.mapping.model.SimpleTypeHolder; +import org.springframework.data.relational.core.dialect.Dialect; import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.repository.CrudRepository; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.test.jdbc.JdbcTestUtils; +import java.util.Collections; + /** * Very simple use cases for creation and usage of JdbcRepositories. * @@ -52,13 +64,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests { @Configuration @Import(TestConfiguration.class) @EnableJdbcRepositories(considerNestedRepositories = true, - includeFilters = @ComponentScan.Filter(value = Ones.class, type = FilterType.ASSIGNABLE_TYPE)) + includeFilters = @ComponentScan.Filter(value = {Ones.class, ReferencingAggregateRepository.class}, type = FilterType.ASSIGNABLE_TYPE)) static class Config { + @Bean + JdbcCustomConversions jdbcCustomConversions() { + return new JdbcCustomConversions(asList( AggregateIdToLong.INSTANCE, LongToAggregateId.INSTANCE )); + } } @Autowired NamedParameterJdbcTemplate template; @Autowired Ones ones; + @Autowired ReferencingAggregateRepository referencingAggregates; @Autowired RelationalMappingContext context; @SuppressWarnings("ConstantConditions") @@ -95,6 +112,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests { ).isEqualTo(1); } + @Test // DATAJDBC-221 + public void savesAndReadWithConvertableId() { + + + AggregateReference idReference = AggregateReference.to(new AggregateId(TWO_ID)); + ReferencingAggregate reference = referencingAggregates.save(new ReferencingAggregate(null, "Reference", idReference)); + + + ReferencingAggregate reloaded = referencingAggregates.findById(reference.id).get(); + assertThat(reloaded.id()).isEqualTo(idReference); + } + interface Ones extends CrudRepository {} static class AggregateOne { @@ -109,4 +138,40 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests { @Id Long id; String name; } + + interface ReferencingAggregateRepository extends CrudRepository { + + } + + record AggregateWithConvertableId(@Id AggregateId id, String name) { + + } + + record AggregateId(Long value) { + + } + + record ReferencingAggregate(@Id Long id, String name, + AggregateReference ref) { + } + + @WritingConverter + enum AggregateIdToLong implements Converter { + INSTANCE; + + @Override + public Long convert(AggregateId source) { + return source.value; + } + } + + @ReadingConverter + enum LongToAggregateId implements Converter< Long,AggregateId> { + INSTANCE; + + @Override + public AggregateId convert(Long source) { + return new AggregateId(source); + } + } } diff --git a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests-hsql.sql b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests-hsql.sql index f03df7b7e..e0a4ba3b6 100644 --- a/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests-hsql.sql +++ b/spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests-hsql.sql @@ -1 +1,8 @@ CREATE TABLE aggregate_one ( id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, NAME VARCHAR(100), two INTEGER); + +CREATE TABLE REFERENCING_AGGREGATE +( + ID BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, + NAME VARCHAR(100), + REF INTEGER +);