27
27
import org .apache .commons .logging .Log ;
28
28
import org .apache .commons .logging .LogFactory ;
29
29
import org .springframework .context .ApplicationContextAware ;
30
- import org .springframework .core .convert .ConverterNotFoundException ;
31
30
import org .springframework .core .convert .converter .Converter ;
32
- import org .springframework .core .convert .converter .ConverterRegistry ;
33
31
import org .springframework .data .convert .CustomConversions ;
34
32
import org .springframework .data .jdbc .core .mapping .AggregateReference ;
35
33
import org .springframework .data .jdbc .core .mapping .JdbcValue ;
@@ -80,7 +78,7 @@ public class MappingJdbcConverter extends MappingRelationalConverter implements
80
78
* {@link #MappingJdbcConverter(RelationalMappingContext, RelationResolver, CustomConversions, JdbcTypeFactory)}
81
79
* (MappingContext, RelationResolver, JdbcTypeFactory)} to convert arrays and large objects into JDBC-specific types.
82
80
*
83
- * @param context must not be {@literal null}.
81
+ * @param context must not be {@literal null}.
84
82
* @param relationResolver used to fetch additional relations from the database. Must not be {@literal null}.
85
83
*/
86
84
public MappingJdbcConverter (RelationalMappingContext context , RelationResolver relationResolver ) {
@@ -91,19 +89,17 @@ public MappingJdbcConverter(RelationalMappingContext context, RelationResolver r
91
89
92
90
this .typeFactory = JdbcTypeFactory .unsupported ();
93
91
this .relationResolver = relationResolver ;
94
-
95
- registerAggregateReferenceConverters ();
96
92
}
97
93
98
94
/**
99
95
* Creates a new {@link MappingJdbcConverter} given {@link MappingContext}.
100
96
*
101
- * @param context must not be {@literal null}.
97
+ * @param context must not be {@literal null}.
102
98
* @param relationResolver used to fetch additional relations from the database. Must not be {@literal null}.
103
- * @param typeFactory must not be {@literal null}
99
+ * @param typeFactory must not be {@literal null}
104
100
*/
105
101
public MappingJdbcConverter (RelationalMappingContext context , RelationResolver relationResolver ,
106
- CustomConversions conversions , JdbcTypeFactory typeFactory ) {
102
+ CustomConversions conversions , JdbcTypeFactory typeFactory ) {
107
103
108
104
super (context , conversions );
109
105
@@ -112,14 +108,6 @@ public MappingJdbcConverter(RelationalMappingContext context, RelationResolver r
112
108
113
109
this .typeFactory = typeFactory ;
114
110
this .relationResolver = relationResolver ;
115
-
116
- registerAggregateReferenceConverters ();
117
- }
118
-
119
- private void registerAggregateReferenceConverters () {
120
-
121
- ConverterRegistry registry = (ConverterRegistry ) getConversionService ();
122
- AggregateReferenceConverters .getConvertersToRegister (getConversionService ()).forEach (registry ::addConverter );
123
111
}
124
112
125
113
@ Nullable
@@ -185,33 +173,48 @@ private Class<?> doGetColumnType(RelationalPersistentProperty property) {
185
173
}
186
174
187
175
@ Override
188
- @ Nullable
189
- public Object readValue (@ Nullable Object value , TypeInformation <?> type ) {
190
-
191
- if (value == null ) {
192
- return value ;
193
- }
176
+ protected Object readTechnologyType (Object value ) {
194
177
195
178
if (value instanceof Array array ) {
196
179
try {
197
- return super . readValue ( array .getArray (), type );
198
- } catch (SQLException | ConverterNotFoundException e ) {
180
+ return array .getArray ();
181
+ } catch (SQLException e ) {
199
182
LOG .info ("Failed to extract a value of type %s from an Array; Attempting to use standard conversions" , e );
183
+
200
184
}
201
185
}
202
186
203
- return super .readValue (value , type );
187
+ return value ;
188
+ }
189
+
190
+ @ Override
191
+ protected TypeInformation <?> determineModuleReadTarget (TypeInformation <?> ultimateTargetType ) {
192
+
193
+ if (AggregateReference .class .isAssignableFrom (ultimateTargetType .getType ())) {
194
+ // the id type of a AggregateReference
195
+ return ultimateTargetType .getTypeArguments ().get (1 );
196
+ }
197
+ return ultimateTargetType ;
204
198
}
205
199
206
200
@ Override
201
+ protected Object readModuleType (Object value , TypeInformation <?> targetType ) {
202
+
203
+ if (AggregateReference .class .isAssignableFrom (targetType .getType ())) {
204
+ return AggregateReference .to (value );
205
+ }
206
+ return value ;
207
+ }
208
+
207
209
@ Nullable
208
- public Object writeValue (@ Nullable Object value , TypeInformation <?> type ) {
210
+ @ Override
211
+ protected Object getPotentiallyConvertedSimpleWrite (Object value , TypeInformation <?> type ) {
209
212
210
- if (value == null ) {
211
- return null ;
213
+ if (value instanceof AggregateReference <?, ?> aggregateReference ) {
214
+ return writeValue ( aggregateReference . getId (), type ) ;
212
215
}
213
216
214
- return super .writeValue (value , type );
217
+ return super .getPotentiallyConvertedSimpleWrite (value , type );
215
218
}
216
219
217
220
private boolean canWriteAsJdbcValue (@ Nullable Object value ) {
@@ -285,7 +288,7 @@ public <R> R readAndResolve(TypeInformation<R> type, RowDocument source, Identif
285
288
286
289
@ Override
287
290
protected RelationalPropertyValueProvider newValueProvider (RowDocumentAccessor documentAccessor ,
288
- ValueExpressionEvaluator evaluator , ConversionContext context ) {
291
+ ValueExpressionEvaluator evaluator , ConversionContext context ) {
289
292
290
293
if (context instanceof ResolvingConversionContext rcc ) {
291
294
@@ -314,7 +317,7 @@ class ResolvingRelationalPropertyValueProvider implements RelationalPropertyValu
314
317
private final Identifier identifier ;
315
318
316
319
private ResolvingRelationalPropertyValueProvider (AggregatePathValueProvider delegate , RowDocumentAccessor accessor ,
317
- ResolvingConversionContext context , Identifier identifier ) {
320
+ ResolvingConversionContext context , Identifier identifier ) {
318
321
319
322
AggregatePath path = context .aggregatePath ();
320
323
@@ -323,15 +326,15 @@ private ResolvingRelationalPropertyValueProvider(AggregatePathValueProvider dele
323
326
this .context = context ;
324
327
this .identifier = path .isEntity ()
325
328
? potentiallyAppendIdentifier (identifier , path .getRequiredLeafEntity (),
326
- property -> delegate .getValue (path .append (property )))
329
+ property -> delegate .getValue (path .append (property )))
327
330
: identifier ;
328
331
}
329
332
330
333
/**
331
334
* Conditionally append the identifier if the entity has an identifier property.
332
335
*/
333
336
static Identifier potentiallyAppendIdentifier (Identifier base , RelationalPersistentEntity <?> entity ,
334
- Function <RelationalPersistentProperty , Object > getter ) {
337
+ Function <RelationalPersistentProperty , Object > getter ) {
335
338
336
339
if (entity .hasIdProperty ()) {
337
340
@@ -460,7 +463,7 @@ public RelationalPropertyValueProvider withContext(ConversionContext context) {
460
463
461
464
return context == this .context ? this
462
465
: new ResolvingRelationalPropertyValueProvider (delegate .withContext (context ), accessor ,
463
- (ResolvingConversionContext ) context , identifier );
466
+ (ResolvingConversionContext ) context , identifier );
464
467
}
465
468
}
466
469
@@ -472,7 +475,7 @@ public RelationalPropertyValueProvider withContext(ConversionContext context) {
472
475
* @param identifier
473
476
*/
474
477
private record ResolvingConversionContext (ConversionContext delegate , AggregatePath aggregatePath ,
475
- Identifier identifier ) implements ConversionContext {
478
+ Identifier identifier ) implements ConversionContext {
476
479
477
480
@ Override
478
481
public <S > S convert (Object source , TypeInformation <? extends S > typeHint ) {
0 commit comments