15
15
*/
16
16
package org .springframework .data .jdbc .repository ;
17
17
18
+ import static java .util .Arrays .*;
18
19
import static org .assertj .core .api .Assertions .*;
19
20
20
21
import org .junit .jupiter .api .Test ;
21
22
import org .springframework .beans .factory .annotation .Autowired ;
23
+ import org .springframework .context .annotation .Bean ;
22
24
import org .springframework .context .annotation .ComponentScan ;
23
25
import org .springframework .context .annotation .Configuration ;
24
26
import org .springframework .context .annotation .FilterType ;
25
27
import org .springframework .context .annotation .Import ;
28
+ import org .springframework .core .convert .converter .Converter ;
26
29
import 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 ;
27
34
import org .springframework .data .jdbc .core .mapping .AggregateReference ;
35
+ import org .springframework .data .jdbc .core .mapping .JdbcSimpleTypes ;
28
36
import org .springframework .data .jdbc .repository .config .EnableJdbcRepositories ;
29
37
import org .springframework .data .jdbc .testing .DatabaseType ;
30
38
import org .springframework .data .jdbc .testing .EnabledOnDatabase ;
31
39
import org .springframework .data .jdbc .testing .IntegrationTest ;
32
40
import org .springframework .data .jdbc .testing .TestConfiguration ;
41
+ import org .springframework .data .mapping .model .SimpleTypeHolder ;
42
+ import org .springframework .data .relational .core .dialect .Dialect ;
33
43
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
34
44
import org .springframework .data .repository .CrudRepository ;
35
45
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcTemplate ;
36
46
import org .springframework .test .jdbc .JdbcTestUtils ;
37
47
48
+ import java .util .Collections ;
49
+
38
50
/**
39
51
* Very simple use cases for creation and usage of JdbcRepositories.
40
52
*
@@ -52,13 +64,18 @@ public class JdbcRepositoryCrossAggregateHsqlIntegrationTests {
52
64
@ Configuration
53
65
@ Import (TestConfiguration .class )
54
66
@ 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 ))
56
68
static class Config {
57
69
70
+ @ Bean
71
+ JdbcCustomConversions jdbcCustomConversions () {
72
+ return new JdbcCustomConversions (asList ( AggregateIdToLong .INSTANCE , LongToAggregateId .INSTANCE ));
73
+ }
58
74
}
59
75
60
76
@ Autowired NamedParameterJdbcTemplate template ;
61
77
@ Autowired Ones ones ;
78
+ @ Autowired ReferencingAggregateRepository referencingAggregates ;
62
79
@ Autowired RelationalMappingContext context ;
63
80
64
81
@ SuppressWarnings ("ConstantConditions" )
@@ -95,6 +112,18 @@ public void savesAndUpdate() {
95
112
).isEqualTo (1 );
96
113
}
97
114
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
+
98
127
interface Ones extends CrudRepository <AggregateOne , Long > {}
99
128
100
129
static class AggregateOne {
@@ -109,4 +138,40 @@ static class AggregateTwo {
109
138
@ Id Long id ;
110
139
String name ;
111
140
}
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
+ }
112
177
}
0 commit comments