Skip to content

Commit 9e8e3ce

Browse files
committed
Move AggregateReference converter registration into MappingJdbcConverter.
See #1750 Original pull request: #1785
1 parent 284509a commit 9e8e3ce

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

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

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,8 @@
3939
*/
4040
public class JdbcCustomConversions extends CustomConversions {
4141

42-
private static final Collection<Object> STORE_CONVERTERS;
43-
44-
static {
45-
46-
List<Object> converters = new ArrayList<>(Jsr310TimestampBasedConverters.getConvertersToRegister());
47-
48-
converters
49-
.addAll(AggregateReferenceConverters.getConvertersToRegister(DefaultConversionService.getSharedInstance()));
50-
51-
STORE_CONVERTERS = Collections.unmodifiableCollection(converters);
52-
53-
}
42+
private static final Collection<Object> STORE_CONVERTERS = Collections
43+
.unmodifiableCollection(Jsr310TimestampBasedConverters.getConvertersToRegister());
5444

5545
/**
5646
* Creates an empty {@link JdbcCustomConversions} object.
@@ -66,7 +56,6 @@ public JdbcCustomConversions() {
6656
* @param converters must not be {@literal null}.
6757
*/
6858
public JdbcCustomConversions(List<?> converters) {
69-
7059
super(constructConverterConfiguration(converters));
7160
}
7261

@@ -98,29 +87,13 @@ public JdbcCustomConversions(ConverterConfiguration converterConfiguration) {
9887

9988
private static ConverterConfiguration constructConverterConfiguration(List<?> converters) {
10089

101-
StoreConversions storeConversions = storeConversions(converters);
102-
10390
return new ConverterConfiguration( //
104-
storeConversions, //
91+
StoreConversions.of(JdbcSimpleTypes.HOLDER, STORE_CONVERTERS), //
10592
converters, //
10693
JdbcCustomConversions::excludeConversionsBetweenDateAndJsr310Types //
10794
);
10895
}
10996

110-
private static StoreConversions storeConversions(List<?> userConverters) {
111-
112-
List<Object> converters = new ArrayList<>(Jsr310TimestampBasedConverters.getConvertersToRegister());
113-
114-
DefaultConversionService defaultConversionService = new DefaultConversionService();
115-
for (Object userConverter : userConverters) {
116-
if (userConverter instanceof Converter<?, ?> converter)
117-
defaultConversionService.addConverter(converter);
118-
}
119-
120-
converters.addAll(AggregateReferenceConverters.getConvertersToRegister(defaultConversionService));
121-
122-
return StoreConversions.of(JdbcSimpleTypes.HOLDER, Collections.unmodifiableCollection(converters));
123-
}
12497

12598
/**
12699
* Obtain a read only copy of default store converters.

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.context.ApplicationContextAware;
3030
import org.springframework.core.convert.ConverterNotFoundException;
3131
import org.springframework.core.convert.converter.Converter;
32+
import org.springframework.core.convert.converter.ConverterRegistry;
3233
import org.springframework.data.convert.CustomConversions;
3334
import org.springframework.data.jdbc.core.mapping.AggregateReference;
3435
import org.springframework.data.jdbc.core.mapping.JdbcValue;
@@ -91,6 +92,8 @@ public MappingJdbcConverter(RelationalMappingContext context, RelationResolver r
9192

9293
this.typeFactory = JdbcTypeFactory.unsupported();
9394
this.relationResolver = relationResolver;
95+
96+
registerAggregateReferenceConverters();
9497
}
9598

9699
/**
@@ -110,6 +113,14 @@ public MappingJdbcConverter(RelationalMappingContext context, RelationResolver r
110113

111114
this.typeFactory = typeFactory;
112115
this.relationResolver = relationResolver;
116+
117+
registerAggregateReferenceConverters();
118+
}
119+
120+
private void registerAggregateReferenceConverters() {
121+
122+
ConverterRegistry registry = (ConverterRegistry) getConversionService();
123+
AggregateReferenceConverters.getConvertersToRegister(getConversionService()).forEach(registry::addConverter);
113124
}
114125

115126
@Nullable
@@ -327,7 +338,8 @@ private ResolvingRelationalPropertyValueProvider(AggregatePathValueProvider dele
327338
this.accessor = accessor;
328339
this.context = context;
329340
this.identifier = path.isEntity()
330-
? potentiallyAppendIdentifier(identifier, path.getRequiredLeafEntity(), property -> delegate.getValue(path.append(property)))
341+
? potentiallyAppendIdentifier(identifier, path.getRequiredLeafEntity(),
342+
property -> delegate.getValue(path.append(property)))
331343
: identifier;
332344
}
333345

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public MappingRelationalConverter(RelationalMappingContext context, CustomConver
120120

121121
this.spELContext = new SpELContext(DocumentPropertyAccessor.INSTANCE);
122122
this.introspector = createIntrospector(projectionFactory, getConversions(), getMappingContext());
123-
124123
}
125124

126125
private static EntityProjectionIntrospector createIntrospector(ProjectionFactory projectionFactory,

0 commit comments

Comments
 (0)