1515 */
1616package org .springframework .data .jdbc .repository ;
1717
18+ import static java .util .Arrays .*;
1819import static org .assertj .core .api .Assertions .*;
1920
2021import org .junit .jupiter .api .Test ;
2122import org .springframework .beans .factory .annotation .Autowired ;
23+ import org .springframework .context .annotation .Bean ;
2224import org .springframework .context .annotation .ComponentScan ;
2325import org .springframework .context .annotation .Configuration ;
2426import org .springframework .context .annotation .FilterType ;
2527import org .springframework .context .annotation .Import ;
28+ import org .springframework .core .convert .converter .Converter ;
2629import org .springframework .data .annotation .Id ;
30+ import org .springframework .data .convert .CustomConversions ;
31+ import org .springframework .data .convert .ReadingConverter ;
32+ import org .springframework .data .convert .WritingConverter ;
33+ import org .springframework .data .jdbc .core .convert .JdbcCustomConversions ;
2734import org .springframework .data .jdbc .core .mapping .AggregateReference ;
35+ import org .springframework .data .jdbc .core .mapping .JdbcSimpleTypes ;
2836import org .springframework .data .jdbc .repository .config .EnableJdbcRepositories ;
2937import org .springframework .data .jdbc .testing .DatabaseType ;
3038import org .springframework .data .jdbc .testing .EnabledOnDatabase ;
3139import org .springframework .data .jdbc .testing .IntegrationTest ;
3240import org .springframework .data .jdbc .testing .TestConfiguration ;
41+ import org .springframework .data .mapping .model .SimpleTypeHolder ;
42+ import org .springframework .data .relational .core .dialect .Dialect ;
3343import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
3444import org .springframework .data .repository .CrudRepository ;
3545import org .springframework .jdbc .core .namedparam .NamedParameterJdbcTemplate ;
3646import org .springframework .test .jdbc .JdbcTestUtils ;
3747
48+ import java .util .Collections ;
49+
3850/**
3951 * Very simple use cases for creation and usage of JdbcRepositories.
4052 *
@@ -52,13 +64,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
5264 @ Configuration
5365 @ Import (TestConfiguration .class )
5466 @ EnableJdbcRepositories (considerNestedRepositories = true ,
55- includeFilters = @ ComponentScan .Filter (value = Ones .class , type = FilterType .ASSIGNABLE_TYPE ))
67+ includeFilters = @ ComponentScan .Filter (value = { Ones .class , ReferencingAggregateRepository . class } , type = FilterType .ASSIGNABLE_TYPE ))
5668 static class Config {
5769
70+ @ Bean
71+ JdbcCustomConversions jdbcCustomConversions () {
72+ return new JdbcCustomConversions (asList ( AggregateIdToLong .INSTANCE , LongToAggregateId .INSTANCE ));
73+ }
5874 }
5975
6076 @ Autowired NamedParameterJdbcTemplate template ;
6177 @ Autowired Ones ones ;
78+ @ Autowired ReferencingAggregateRepository referencingAggregates ;
6279 @ Autowired RelationalMappingContext context ;
6380
6481 @ SuppressWarnings ("ConstantConditions" )
@@ -95,6 +112,18 @@ public void savesAndUpdate() {
95112 ).isEqualTo (1 );
96113 }
97114
115+ @ Test // DATAJDBC-221
116+ public void savesAndReadWithConvertableId () {
117+
118+
119+ AggregateReference <AggregateWithConvertableId , AggregateId > idReference = AggregateReference .to (new AggregateId (TWO_ID ));
120+ ReferencingAggregate reference = referencingAggregates .save (new ReferencingAggregate (null , "Reference" , idReference ));
121+
122+
123+ ReferencingAggregate reloaded = referencingAggregates .findById (reference .id ).get ();
124+ assertThat (reloaded .id ()).isEqualTo (idReference );
125+ }
126+
98127 interface Ones extends CrudRepository <AggregateOne , Long > {}
99128
100129 static class AggregateOne {
@@ -109,4 +138,40 @@ static class AggregateTwo {
109138 @ Id Long id ;
110139 String name ;
111140 }
141+
142+ interface ReferencingAggregateRepository extends CrudRepository <ReferencingAggregate , Long > {
143+
144+ }
145+
146+ record AggregateWithConvertableId (@ Id AggregateId id , String name ) {
147+
148+ }
149+
150+ record AggregateId (Long value ) {
151+
152+ }
153+
154+ record ReferencingAggregate (@ Id Long id , String name ,
155+ AggregateReference <AggregateWithConvertableId , AggregateId > ref ) {
156+ }
157+
158+ @ WritingConverter
159+ enum AggregateIdToLong implements Converter <AggregateId , Long > {
160+ INSTANCE ;
161+
162+ @ Override
163+ public Long convert (AggregateId source ) {
164+ return source .value ;
165+ }
166+ }
167+
168+ @ ReadingConverter
169+ enum LongToAggregateId implements Converter < Long ,AggregateId > {
170+ INSTANCE ;
171+
172+ @ Override
173+ public AggregateId convert (Long source ) {
174+ return new AggregateId (source );
175+ }
176+ }
112177}
0 commit comments