Skip to content

Commit ef1a0cb

Browse files
committed
Fixed writing AggregateRef with custom write target.
The TODO mentioned in review was already resolved. Added the proper conversion step during writing. Fixed the wrong assumption in the test, that the database will return a Long. Original pull request #2062 See #1828
1 parent a891485 commit ef1a0cb

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public Object readValue(@Nullable Object value, TypeInformation<?> targetType) {
193193
value = readJdbcArray(value);
194194
targetType = determineNestedTargetType(targetType);
195195

196-
return readToAggregateReference(getPotentiallyConvertedSimpleRead(value, targetType), originalTargetType);
196+
return possiblyReadToAggregateReference(getPotentiallyConvertedSimpleRead(value, targetType), originalTargetType);
197197
}
198198

199199
/**
@@ -228,7 +228,7 @@ private TypeInformation<?> determineNestedTargetType(TypeInformation<?> ultimate
228228
/**
229229
* Convert value to an {@link AggregateReference} if that is specified by the parameter targetType.
230230
*/
231-
private Object readToAggregateReference(Object value, TypeInformation<?> targetType) {
231+
private Object possiblyReadToAggregateReference(Object value, TypeInformation<?> targetType) {
232232

233233
if (AggregateReference.class.isAssignableFrom(targetType.getType())) {
234234
return AggregateReference.to(value);
@@ -277,28 +277,37 @@ private boolean canWriteAsJdbcValue(@Nullable Object value) {
277277
public JdbcValue writeJdbcValue(@Nullable Object value, TypeInformation<?> columnType, SQLType sqlType) {
278278

279279
TypeInformation<?> targetType = canWriteAsJdbcValue(value) ? TypeInformation.of(JdbcValue.class) : columnType;
280+
281+
if (value instanceof AggregateReference<?, ?> aggregateReference) {
282+
return writeJdbcValue(aggregateReference.getId(), columnType, sqlType);
283+
}
284+
280285
Object convertedValue = writeValue(value, targetType);
281286

282287
if (convertedValue instanceof JdbcValue result) {
283288
return result;
284289
}
285290

286-
if (convertedValue == null || !convertedValue.getClass().isArray()) {
287-
return JdbcValue.of(convertedValue, sqlType);
291+
if (convertedValue == null) {
292+
return JdbcValue.of(null, sqlType);
288293
}
289294

290-
Class<?> componentType = convertedValue.getClass().getComponentType();
291-
if (componentType != byte.class && componentType != Byte.class) {
295+
if (convertedValue.getClass().isArray()) {// array conversion
296+
Class<?> componentType = convertedValue.getClass().getComponentType();
297+
if (componentType != byte.class && componentType != Byte.class) {
292298

293-
Object[] objectArray = requireObjectArray(convertedValue);
294-
return JdbcValue.of(typeFactory.createArray(objectArray), JDBCType.ARRAY);
295-
}
299+
Object[] objectArray = requireObjectArray(convertedValue);
300+
return JdbcValue.of(typeFactory.createArray(objectArray), JDBCType.ARRAY);
301+
}
296302

297-
if (componentType == Byte.class) {
298-
convertedValue = ArrayUtils.toPrimitive((Byte[]) convertedValue);
299-
}
303+
if (componentType == Byte.class) {
304+
convertedValue = ArrayUtils.toPrimitive((Byte[]) convertedValue);
305+
}
300306

301-
return JdbcValue.of(convertedValue, JDBCType.BINARY);
307+
return JdbcValue.of(convertedValue, JDBCType.BINARY);
308+
}
309+
310+
return JdbcValue.of(convertedValue, sqlType);
302311
}
303312

304313
@SuppressWarnings("unchecked")

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryCrossAggregateHsqlIntegrationTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static class Config {
6565

6666
@Bean
6767
JdbcCustomConversions jdbcCustomConversions() {
68-
return new JdbcCustomConversions(asList(AggregateIdToLong.INSTANCE, LongToAggregateId.INSTANCE));
68+
return new JdbcCustomConversions(asList(AggregateIdToLong.INSTANCE, NumberToAggregateId.INSTANCE));
6969
}
7070
}
7171

@@ -109,7 +109,6 @@ public void savesAndUpdate() {
109109
}
110110

111111
@Test // GH-1828
112-
@Disabled
113112
public void savesAndReadWithConvertableId() {
114113

115114
AggregateReference<AggregateWithConvertableId, AggregateId> idReference = AggregateReference
@@ -118,7 +117,7 @@ public void savesAndReadWithConvertableId() {
118117
.save(new ReferencingAggregate(null, "Reference", idReference));
119118

120119
ReferencingAggregate reloaded = referencingAggregates.findById(reference.id).get();
121-
assertThat(reloaded.id()).isEqualTo(idReference);
120+
assertThat(reloaded.ref()).isEqualTo(idReference);
122121
}
123122

124123
interface Ones extends CrudRepository<AggregateOne, Long> {}
@@ -163,12 +162,12 @@ public Long convert(AggregateId source) {
163162
}
164163

165164
@ReadingConverter
166-
enum LongToAggregateId implements Converter<Long, AggregateId> {
165+
enum NumberToAggregateId implements Converter<Number, AggregateId> {
167166
INSTANCE;
168167

169168
@Override
170-
public AggregateId convert(Long source) {
171-
return new AggregateId(source);
169+
public AggregateId convert(Number source) {
170+
return new AggregateId(source.longValue());
172171
}
173172
}
174173
}

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,7 @@
4444
import org.springframework.data.mapping.PersistentPropertyAccessor;
4545
import org.springframework.data.mapping.PersistentPropertyPathAccessor;
4646
import org.springframework.data.mapping.context.MappingContext;
47-
import org.springframework.data.mapping.model.CachingValueExpressionEvaluatorFactory;
48-
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
49-
import org.springframework.data.mapping.model.EntityInstantiator;
50-
import org.springframework.data.mapping.model.ParameterValueProvider;
51-
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
52-
import org.springframework.data.mapping.model.PropertyValueProvider;
53-
import org.springframework.data.mapping.model.SimpleTypeHolder;
54-
import org.springframework.data.mapping.model.SpELContext;
55-
import org.springframework.data.mapping.model.ValueExpressionEvaluator;
56-
import org.springframework.data.mapping.model.ValueExpressionParameterValueProvider;
47+
import org.springframework.data.mapping.model.*;
5748
import org.springframework.data.projection.EntityProjection;
5849
import org.springframework.data.projection.EntityProjectionIntrospector;
5950
import org.springframework.data.projection.EntityProjectionIntrospector.ProjectionPredicate;
@@ -1174,8 +1165,6 @@ public Object getValue(AggregatePath path) {
11741165
return null;
11751166
}
11761167

1177-
// TODO: converting here seems wrong, since we have the ConvertingParameterValueProvider
1178-
// return context.convert(value, path.getRequiredLeafProperty().getTypeInformation());
11791168
return value;
11801169
}
11811170

0 commit comments

Comments
 (0)