4040import org .junit .jupiter .api .Test ;
4141import org .springframework .core .convert .converter .Converter ;
4242import org .springframework .data .annotation .Id ;
43+ import org .springframework .data .convert .WritingConverter ;
4344import org .springframework .data .jdbc .core .mapping .AggregateReference ;
4445import org .springframework .data .jdbc .core .mapping .JdbcMappingContext ;
4546import org .springframework .data .jdbc .core .mapping .JdbcValue ;
@@ -60,8 +61,7 @@ class MappingJdbcConverterUnitTests {
6061
6162 private static final UUID UUID = java .util .UUID .fromString ("87a48aa8-a071-705e-54a9-e52fe3a012f1" );
6263 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 };
6565
6666 private JdbcMappingContext context = new JdbcMappingContext ();
6767 private StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory ();
@@ -70,7 +70,7 @@ class MappingJdbcConverterUnitTests {
7070 (identifier , path ) -> {
7171 throw new UnsupportedOperationException ();
7272 }, //
73- new JdbcCustomConversions (), //
73+ new JdbcCustomConversions (List . of ( CustomIdToLong . INSTANCE ) ), //
7474 typeFactory //
7575 );
7676
@@ -91,6 +91,7 @@ void testTargetTypesForPropertyType() {
9191 checkTargetType (softly , entity , "date" , Date .class );
9292 checkTargetType (softly , entity , "timestamp" , Timestamp .class );
9393 checkTargetType (softly , entity , "uuid" , UUID .class );
94+ checkTargetType (softly , entity , "reference" , Long .class );
9495
9596 softly .assertAll ();
9697 }
@@ -216,117 +217,26 @@ private void checkTargetType(SoftAssertions softly, RelationalPersistentEntity<?
216217 }
217218
218219 @ 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+ ) {
330240 }
331241
332242 @ SuppressWarnings ("unused" )
@@ -337,6 +247,18 @@ private enum SomeEnum {
337247 @ SuppressWarnings ("unused" )
338248 private static class OtherEntity {}
339249
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+
340262 private static class StubbedJdbcTypeFactory implements JdbcTypeFactory {
341263 Object [] arraySource ;
342264
@@ -366,4 +288,14 @@ public UUID convert(byte[] source) {
366288 return new UUID (high , low );
367289 }
368290 }
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+ }
369301}
0 commit comments