15
15
*/
16
16
package org .springframework .data .jdbc .core .convert ;
17
17
18
+ import static java .util .Arrays .*;
18
19
import static org .assertj .core .api .Assertions .*;
19
20
import static org .assertj .core .api .SoftAssertions .*;
20
21
import static org .mockito .Mockito .*;
40
41
import org .junit .jupiter .api .Test ;
41
42
import org .springframework .core .convert .converter .Converter ;
42
43
import org .springframework .data .annotation .Id ;
44
+ import org .springframework .data .convert .WritingConverter ;
43
45
import org .springframework .data .jdbc .core .mapping .AggregateReference ;
44
46
import org .springframework .data .jdbc .core .mapping .JdbcMappingContext ;
45
47
import org .springframework .data .jdbc .core .mapping .JdbcValue ;
@@ -70,7 +72,7 @@ class MappingJdbcConverterUnitTests {
70
72
(identifier , path ) -> {
71
73
throw new UnsupportedOperationException ();
72
74
}, //
73
- new JdbcCustomConversions (), //
75
+ new JdbcCustomConversions (List . of ( CustomIdToLong . INSTANCE ) ), //
74
76
typeFactory //
75
77
);
76
78
@@ -92,6 +94,9 @@ void testTargetTypesForPropertyType() {
92
94
checkTargetType (softly , entity , "date" , Date .class );
93
95
checkTargetType (softly , entity , "timestamp" , Timestamp .class );
94
96
checkTargetType (softly , entity , "uuid" , UUID .class );
97
+ checkTargetType (softly , entity , "reference" , Long .class );
98
+ checkTargetType (softly , entity , "enumIdReference" , String .class );
99
+ checkTargetType (softly , entity , "customIdReference" , Long .class );
95
100
});
96
101
}
97
102
@@ -234,6 +239,8 @@ private record DummyEntity(
234
239
Date date ,
235
240
Timestamp timestamp ,
236
241
AggregateReference <DummyEntity , Long > reference ,
242
+ AggregateReference <EnumIdEntity , SomeEnum > enumIdReference ,
243
+ AggregateReference <CustomIdEntity , SomeEnum > customIdReference ,
237
244
UUID uuid ,
238
245
AggregateReference <ReferencedByUuid , UUID > uuidRef ,
239
246
Optional <UUID > optionalUuid ,
@@ -255,6 +262,18 @@ private enum SomeEnum {
255
262
private static class OtherEntity {
256
263
}
257
264
265
+ private static class EnumIdEntity {
266
+ @ Id SomeEnum id ;
267
+ }
268
+
269
+ private static class CustomIdEntity {
270
+ @ Id CustomId id ;
271
+ }
272
+
273
+ private record CustomId (Long id ) {
274
+
275
+ }
276
+
258
277
private static class StubbedJdbcTypeFactory implements JdbcTypeFactory {
259
278
Object [] arraySource ;
260
279
@@ -284,4 +303,14 @@ public UUID convert(byte[] source) {
284
303
return new UUID (high , low );
285
304
}
286
305
}
306
+
307
+ @ WritingConverter
308
+ private enum CustomIdToLong implements Converter <CustomId , Long > {
309
+ INSTANCE ;
310
+
311
+ @ Override
312
+ public Long convert (CustomId source ) {
313
+ return source .id ;
314
+ }
315
+ }
287
316
}
0 commit comments