40
40
import org .junit .jupiter .api .Test ;
41
41
import org .springframework .core .convert .converter .Converter ;
42
42
import org .springframework .data .annotation .Id ;
43
+ import org .springframework .data .convert .WritingConverter ;
43
44
import org .springframework .data .jdbc .core .mapping .AggregateReference ;
44
45
import org .springframework .data .jdbc .core .mapping .JdbcMappingContext ;
45
46
import org .springframework .data .jdbc .core .mapping .JdbcValue ;
@@ -60,8 +61,7 @@ class MappingJdbcConverterUnitTests {
60
61
61
62
private static final UUID UUID = java .util .UUID .fromString ("87a48aa8-a071-705e-54a9-e52fe3a012f1" );
62
63
private static final byte [] BYTES_REPRESENTING_UUID = { -121 , -92 , -118 , -88 , -96 , 113 , 112 , 94 , 84 , -87 , -27 , 47 ,
63
- -29 ,
64
- -96 , 18 , -15 };
64
+ -29 , -96 , 18 , -15 };
65
65
66
66
private JdbcMappingContext context = new JdbcMappingContext ();
67
67
private StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory ();
@@ -70,7 +70,7 @@ class MappingJdbcConverterUnitTests {
70
70
(identifier , path ) -> {
71
71
throw new UnsupportedOperationException ();
72
72
}, //
73
- new JdbcCustomConversions (), //
73
+ new JdbcCustomConversions (List . of ( CustomIdToLong . INSTANCE ) ), //
74
74
typeFactory //
75
75
);
76
76
@@ -91,6 +91,7 @@ void testTargetTypesForPropertyType() {
91
91
checkTargetType (softly , entity , "date" , Date .class );
92
92
checkTargetType (softly , entity , "timestamp" , Timestamp .class );
93
93
checkTargetType (softly , entity , "uuid" , UUID .class );
94
+ checkTargetType (softly , entity , "reference" , Long .class );
94
95
95
96
softly .assertAll ();
96
97
}
@@ -216,117 +217,26 @@ private void checkTargetType(SoftAssertions softly, RelationalPersistentEntity<?
216
217
}
217
218
218
219
@ 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 ;
235
-
236
- // DATAJDBC-259
237
- private final List <String > listOfString ;
238
- private final String [] arrayOfString ;
239
- private final List <OtherEntity > listOfEntity ;
240
- private final OtherEntity [] arrayOfEntity ;
241
-
242
- private DummyEntity (Long id , SomeEnum someEnum , LocalDateTime localDateTime , LocalDate localDate ,
243
- LocalTime localTime , ZonedDateTime zonedDateTime , OffsetDateTime offsetDateTime , Instant instant , Date date ,
244
- Timestamp timestamp , AggregateReference <DummyEntity , Long > reference , UUID uuid ,
245
- AggregateReference <ReferencedByUuid , UUID > uuidRef , Optional <java .util .UUID > optionalUUID , List <String > listOfString , String [] arrayOfString ,
246
- List <OtherEntity > listOfEntity , OtherEntity [] arrayOfEntity ) {
247
- this .id = id ;
248
- this .someEnum = someEnum ;
249
- this .localDateTime = localDateTime ;
250
- this .localDate = localDate ;
251
- this .localTime = localTime ;
252
- this .zonedDateTime = zonedDateTime ;
253
- this .offsetDateTime = offsetDateTime ;
254
- this .instant = instant ;
255
- this .date = date ;
256
- this .timestamp = timestamp ;
257
- this .reference = reference ;
258
- this .uuid = uuid ;
259
- this .uuidRef = uuidRef ;
260
- this .optionalUuid = optionalUUID ;
261
- this .listOfString = listOfString ;
262
- this .arrayOfString = arrayOfString ;
263
- this .listOfEntity = listOfEntity ;
264
- this .arrayOfEntity = arrayOfEntity ;
265
- }
266
-
267
- public Long getId () {
268
- return this .id ;
269
- }
270
-
271
- public SomeEnum getSomeEnum () {
272
- return this .someEnum ;
273
- }
274
-
275
- public LocalDateTime getLocalDateTime () {
276
- return this .localDateTime ;
277
- }
278
-
279
- public LocalDate getLocalDate () {
280
- return this .localDate ;
281
- }
282
-
283
- public LocalTime getLocalTime () {
284
- return this .localTime ;
285
- }
286
-
287
- public ZonedDateTime getZonedDateTime () {
288
- return this .zonedDateTime ;
289
- }
290
-
291
- public OffsetDateTime getOffsetDateTime () {
292
- return this .offsetDateTime ;
293
- }
294
-
295
- public Instant getInstant () {
296
- return this .instant ;
297
- }
298
-
299
- public Date getDate () {
300
- return this .date ;
301
- }
302
-
303
- public Timestamp getTimestamp () {
304
- return this .timestamp ;
305
- }
306
-
307
- public AggregateReference <DummyEntity , Long > getReference () {
308
- return this .reference ;
309
- }
310
-
311
- public UUID getUuid () {
312
- return this .uuid ;
313
- }
314
-
315
- public List <String > getListOfString () {
316
- return this .listOfString ;
317
- }
318
-
319
- public String [] getArrayOfString () {
320
- return this .arrayOfString ;
321
- }
322
-
323
- public List <OtherEntity > getListOfEntity () {
324
- return this .listOfEntity ;
325
- }
326
-
327
- public OtherEntity [] getArrayOfEntity () {
328
- return this .arrayOfEntity ;
329
- }
220
+ private record DummyEntity ( //
221
+ @ Id Long id , //
222
+ SomeEnum someEnum , //
223
+ LocalDateTime localDateTime , //
224
+ LocalDate localDate , //
225
+ LocalTime localTime , //
226
+ ZonedDateTime zonedDateTime , //
227
+ OffsetDateTime offsetDateTime , //
228
+ Instant instant , //
229
+ Date date , //
230
+ Timestamp timestamp , //
231
+ AggregateReference <DummyEntity , Long > reference , //
232
+ UUID uuid , //
233
+ AggregateReference <ReferencedByUuid , UUID > uuidRef , //
234
+ Optional <UUID > optionalUuid , //
235
+ List <String > listOfString , //
236
+ String [] arrayOfString , //
237
+ List <OtherEntity > listOfEntity , //
238
+ OtherEntity [] arrayOfEntity //
239
+ ) {
330
240
}
331
241
332
242
@ SuppressWarnings ("unused" )
@@ -337,6 +247,18 @@ private enum SomeEnum {
337
247
@ SuppressWarnings ("unused" )
338
248
private static class OtherEntity {}
339
249
250
+ private static class EnumIdEntity {
251
+ @ Id SomeEnum id ;
252
+ }
253
+
254
+ private static class CustomIdEntity {
255
+ @ Id CustomId id ;
256
+ }
257
+
258
+ private record CustomId (Long id ) {
259
+
260
+ }
261
+
340
262
private static class StubbedJdbcTypeFactory implements JdbcTypeFactory {
341
263
Object [] arraySource ;
342
264
@@ -366,4 +288,14 @@ public UUID convert(byte[] source) {
366
288
return new UUID (high , low );
367
289
}
368
290
}
291
+
292
+ @ WritingConverter
293
+ enum CustomIdToLong implements Converter <CustomId , Long > {
294
+ INSTANCE ;
295
+
296
+ @ Override
297
+ public Long convert (CustomId source ) {
298
+ return source .id ;
299
+ }
300
+ }
369
301
}
0 commit comments