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
@@ -91,6 +93,9 @@ void testTargetTypesForPropertyType() {
91
93
checkTargetType (softly , entity , "date" , Date .class );
92
94
checkTargetType (softly , entity , "timestamp" , Timestamp .class );
93
95
checkTargetType (softly , entity , "uuid" , UUID .class );
96
+ checkTargetType (softly , entity , "reference" , Long .class );
97
+ checkTargetType (softly , entity , "enumIdReference" , String .class );
98
+ checkTargetType (softly , entity , "customIdReference" , Long .class );
94
99
95
100
softly .assertAll ();
96
101
}
@@ -216,22 +221,21 @@ private void checkTargetType(SoftAssertions softly, RelationalPersistentEntity<?
216
221
}
217
222
218
223
@ SuppressWarnings ("unused" )
219
- private static class DummyEntity {
220
-
221
- @ Id private final Long id ;
222
- private final SomeEnum someEnum ;
223
- private final LocalDateTime localDateTime ;
224
- private final LocalDate localDate ;
225
- private final LocalTime localTime ;
226
- private final ZonedDateTime zonedDateTime ;
227
- private final OffsetDateTime offsetDateTime ;
228
- private final Instant instant ;
229
- private final Date date ;
230
- private final Timestamp timestamp ;
231
- private final AggregateReference <DummyEntity , Long > reference ;
232
- private final UUID uuid ;
233
- private final AggregateReference <ReferencedByUuid , UUID > uuidRef ;
234
- private final Optional <UUID > optionalUuid ;
224
+ private record DummyEntity (
225
+ @ Id Long id ,
226
+ SomeEnum someEnum ,
227
+ LocalDateTime localDateTime ,
228
+ LocalDate localDate ,
229
+ LocalTime localTime ,
230
+ ZonedDateTime zonedDateTime ,
231
+ OffsetDateTime offsetDateTime ,
232
+ Instant instant ,
233
+ Date date ,
234
+ Timestamp timestamp ,
235
+ AggregateReference <DummyEntity , Long > reference ,
236
+ UUID uuid ,
237
+ AggregateReference <ReferencedByUuid , UUID > uuidRef ,
238
+ Optional <UUID > optionalUuid ,
235
239
236
240
// DATAJDBC-259
237
241
private final List <String > listOfString ;
@@ -337,6 +341,18 @@ private enum SomeEnum {
337
341
@ SuppressWarnings ("unused" )
338
342
private static class OtherEntity {}
339
343
344
+ private static class EnumIdEntity {
345
+ @ Id SomeEnum id ;
346
+ }
347
+
348
+ private static class CustomIdEntity {
349
+ @ Id CustomId id ;
350
+ }
351
+
352
+ private record CustomId (Long id ) {
353
+
354
+ }
355
+
340
356
private static class StubbedJdbcTypeFactory implements JdbcTypeFactory {
341
357
Object [] arraySource ;
342
358
@@ -366,4 +382,14 @@ public UUID convert(byte[] source) {
366
382
return new UUID (high , low );
367
383
}
368
384
}
385
+
386
+ @ WritingConverter
387
+ private enum CustomIdToLong implements Converter <CustomId , Long > {
388
+ INSTANCE ;
389
+
390
+ @ Override
391
+ public Long convert (CustomId source ) {
392
+ return source .id ;
393
+ }
394
+ }
369
395
}
0 commit comments