Skip to content

Commit 483808f

Browse files
committed
Simplified structure of MappingRelationalConverter.readValue().
See #1828 Original pull request #2062
1 parent fe66a43 commit 483808f

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
@@ -609,9 +609,9 @@ private boolean shouldReadEmbeddable(ConversionContext context, RelationalPersis
609609
}
610610

611611
/**
612-
* Read and convert a single value that is comming from a database to the {@literal targetType} expected by the domain
612+
* Read and convert a single value that is coming from a database to the {@literal targetType} expected by the domain
613613
* model.
614-
*
614+
*
615615
* @param value a value as it is returned by the driver accessing the persistence store. May be {@code null}.
616616
* @param targetType {@link TypeInformation} into which the value is to be converted. Must not be {@code null}.
617617
* @return
@@ -624,49 +624,7 @@ public Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
624624
return null;
625625
}
626626

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

672630
/**

0 commit comments

Comments
 (0)