Skip to content

Commit a891485

Browse files
committed
Simplified structure of MappingRelationalConverter.readValue().
See #1828 Original pull request #2062
1 parent 49f26e0 commit a891485

File tree

2 files changed

+38
-54
lines changed

2 files changed

+38
-54
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
import org.apache.commons.logging.Log;
2828
import org.apache.commons.logging.LogFactory;
2929
import org.springframework.context.ApplicationContextAware;
30-
import org.springframework.core.convert.ConversionException;
31-
import org.springframework.core.convert.ConversionFailedException;
3230
import org.springframework.core.convert.converter.Converter;
33-
import org.springframework.dao.DataAccessException;
3431
import org.springframework.dao.NonTransientDataAccessException;
3532
import org.springframework.data.convert.CustomConversions;
3633
import org.springframework.data.jdbc.core.mapping.AggregateReference;
@@ -39,7 +36,6 @@
3936
import org.springframework.data.mapping.context.MappingContext;
4037
import org.springframework.data.mapping.model.SimpleTypeHolder;
4138
import org.springframework.data.mapping.model.ValueExpressionEvaluator;
42-
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
4339
import org.springframework.data.relational.core.conversion.MappingRelationalConverter;
4440
import org.springframework.data.relational.core.conversion.ObjectPath;
4541
import org.springframework.data.relational.core.conversion.RelationalConverter;
@@ -177,8 +173,33 @@ private Class<?> doGetColumnType(RelationalPersistentProperty property) {
177173
return componentColumnType;
178174
}
179175

176+
/**
177+
* Read and convert a single value that is coming from a database to the {@literal targetType} expected by the domain
178+
* model.
179+
*
180+
* @param value a value as it is returned by the driver accessing the persistence store. May be {@code null}.
181+
* @param targetType {@link TypeInformation} into which the value is to be converted. Must not be {@code null}.
182+
* @return
183+
*/
180184
@Override
181-
protected Object readTechnologyType(Object value) {
185+
@Nullable
186+
public Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
187+
188+
if (null == value) {
189+
return null;
190+
}
191+
192+
TypeInformation<?> originalTargetType = targetType;
193+
value = readJdbcArray(value);
194+
targetType = determineNestedTargetType(targetType);
195+
196+
return readToAggregateReference(getPotentiallyConvertedSimpleRead(value, targetType), originalTargetType);
197+
}
198+
199+
/**
200+
* Unwrap a Jdbc array, if such a value is provided
201+
*/
202+
private Object readJdbcArray(Object value) {
182203

183204
if (value instanceof Array array) {
184205
try {
@@ -191,8 +212,11 @@ protected Object readTechnologyType(Object value) {
191212
return value;
192213
}
193214

194-
@Override
195-
protected TypeInformation<?> determineModuleReadTarget(TypeInformation<?> ultimateTargetType) {
215+
/**
216+
* Determine the id type of an {@link AggregateReference} that the rest of the conversion infrastructure needs to use
217+
* as a conversion target.
218+
*/
219+
private TypeInformation<?> determineNestedTargetType(TypeInformation<?> ultimateTargetType) {
196220

197221
if (AggregateReference.class.isAssignableFrom(ultimateTargetType.getType())) {
198222
// the id type of a AggregateReference
@@ -201,8 +225,10 @@ protected TypeInformation<?> determineModuleReadTarget(TypeInformation<?> ultima
201225
return ultimateTargetType;
202226
}
203227

204-
@Override
205-
protected Object readModuleType(Object value, TypeInformation<?> targetType) {
228+
/**
229+
* Convert value to an {@link AggregateReference} if that is specified by the parameter targetType.
230+
*/
231+
private Object readToAggregateReference(Object value, TypeInformation<?> targetType) {
206232

207233
if (AggregateReference.class.isAssignableFrom(targetType.getType())) {
208234
return AggregateReference.to(value);

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,9 @@ private boolean shouldReadEmbeddable(ConversionContext context, RelationalPersis
622622
}
623623

624624
/**
625-
* Read and convert a single value that is comming from a database to the {@literal targetType} expected by the domain
625+
* Read and convert a single value that is coming from a database to the {@literal targetType} expected by the domain
626626
* model.
627-
*
627+
*
628628
* @param value a value as it is returned by the driver accessing the persistence store. May be {@code null}.
629629
* @param targetType {@link TypeInformation} into which the value is to be converted. Must not be {@code null}.
630630
* @return
@@ -637,49 +637,7 @@ public Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
637637
return null;
638638
}
639639

640-
TypeInformation<?> originalTargetType = targetType;
641-
value = readTechnologyType(value);
642-
targetType = determineModuleReadTarget(targetType);
643-
644-
return readModuleType(getPotentiallyConvertedSimpleRead(value, targetType), originalTargetType);
645-
}
646-
647-
/**
648-
* Convert a read value using module dependent special conversions. Spring Data JDBC for example uses this to
649-
* implement the conversion of AggregateReferences. There is no guarantee that the value is converted to the exact
650-
* required TypeInformation, nor that it is converted at all.
651-
*
652-
* @param value the value read from the database. Must not be {@literal null}.
653-
* @param targetType the type to which the value should get converted if possible. Must not be {@literal null}.
654-
* @return a potentially converted value.
655-
*/
656-
protected Object readModuleType(Object value, TypeInformation<?> targetType) {
657-
return value;
658-
}
659-
660-
/**
661-
* Read technology specific values into objects that then can be fed in the normal conversion process for reading. An
662-
* example are the conversion of JDBCs {@literal Array} type to normal java arrays.
663-
*
664-
* @param value a value read from the database
665-
* @return a preprocessed value suitable for technology-agnostic further processing.
666-
*/
667-
protected Object readTechnologyType(Object value) {
668-
return value;
669-
}
670-
671-
/**
672-
* When type is a type that has special support, this returns the type a value should be converted to before the
673-
* conversion to the special type happens. For example if type is AggregateReference this method returns the second
674-
* parameter type of AggregateReference, in order to allow conversions to handle that type.
675-
*
676-
* @param ultimateTargetType ultimate target type to be returned by the conversion process. Must not be
677-
* {@literal null}.
678-
* @return a type that can be converted to the ultimate target type by module specific handling. Must not be
679-
* {@literal null}.
680-
*/
681-
protected TypeInformation<?> determineModuleReadTarget(TypeInformation<?> ultimateTargetType) {
682-
return ultimateTargetType;
640+
return getPotentiallyConvertedSimpleRead(value, targetType);
683641
}
684642

685643
/**

0 commit comments

Comments
 (0)