Skip to content

Commit 64770d3

Browse files
committed
DATAJDBC-102 - Determine the EntityInstantiator to be used dynamically.
Replaced the direct use of EntityInstantiator with EntityInstantiators. Moved it into the MappingContext because instantiation is part of the mapping process.
1 parent 6f333d0 commit 64770d3

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/main/java/org/springframework/data/jdbc/core/EntityRowMapper.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
import org.springframework.core.convert.ConversionService;
2525
import org.springframework.core.convert.converter.Converter;
26-
import org.springframework.data.convert.ClassGeneratingEntityInstantiator;
27-
import org.springframework.data.convert.EntityInstantiator;
26+
import org.springframework.data.convert.EntityInstantiators;
2827
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
2928
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
3029
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
@@ -35,8 +34,6 @@
3534
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
3635
import org.springframework.data.mapping.model.ParameterValueProvider;
3736
import org.springframework.jdbc.core.RowMapper;
38-
import org.springframework.lang.Nullable;
39-
import org.springframework.util.Assert;
4037

4138
/**
4239
* Maps a ResultSet to an entity of type {@code T}, including entities referenced.
@@ -50,7 +47,7 @@ public class EntityRowMapper<T> implements RowMapper<T> {
5047
private static final Converter ITERABLE_OF_ENTRY_TO_MAP_CONVERTER = new IterableOfEntryToMapConverter();
5148

5249
private final JdbcPersistentEntity<T> entity;
53-
private final EntityInstantiator instantiator = new ClassGeneratingEntityInstantiator();
50+
5451
private final ConversionService conversions;
5552
private final JdbcMappingContext context;
5653
private final DataAccessStrategy accessStrategy;
@@ -98,17 +95,17 @@ public T mapRow(ResultSet resultSet, int rowNumber) throws SQLException {
9895
}
9996

10097
private T createInstance(ResultSet rs) {
101-
return instantiator.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, conversions, ""));
102-
}
10398

99+
return context.getInstantiatorFor(entity) //
100+
.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, conversions, ""));
101+
}
104102

105103
/**
106104
* Read a single value or a complete Entity from the {@link ResultSet} passed as an argument.
107105
*
108106
* @param resultSet the {@link ResultSet} to extract the value from. Must not be {@code null}.
109107
* @param property the {@link JdbcPersistentProperty} for which the value is intended. Must not be {@code null}.
110108
* @param prefix to be used for all column names accessed by this method. Must not be {@code null}.
111-
*
112109
* @return the value read from the {@link ResultSet}. May be {@code null}.
113110
*/
114111
private Object readFrom(ResultSet resultSet, JdbcPersistentProperty property, String prefix) {
@@ -138,8 +135,8 @@ private <S> S readEntityFrom(ResultSet rs, PersistentProperty<?> property) {
138135
return null;
139136
}
140137

141-
S instance = instantiator.createInstance(entity,
142-
new ResultSetParameterValueProvider(rs, entity, conversions, prefix));
138+
S instance = context.getInstantiatorFor(entity) //
139+
.createInstance(entity, new ResultSetParameterValueProvider(rs, entity, conversions, prefix));
143140

144141
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance);
145142
ConvertingPropertyAccessor propertyAccessor = new ConvertingPropertyAccessor(accessor, conversions);

src/main/java/org/springframework/data/jdbc/mapping/model/JdbcMappingContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
import java.util.HashSet;
2828
import java.util.List;
2929

30+
import lombok.Setter;
3031
import org.springframework.core.convert.ConversionService;
3132
import org.springframework.core.convert.support.DefaultConversionService;
3233
import org.springframework.core.convert.support.GenericConversionService;
34+
import org.springframework.data.convert.EntityInstantiator;
35+
import org.springframework.data.convert.EntityInstantiators;
3336
import org.springframework.data.convert.Jsr310Converters;
37+
import org.springframework.data.mapping.PersistentEntity;
3438
import org.springframework.data.mapping.PropertyPath;
3539
import org.springframework.data.mapping.context.AbstractMappingContext;
3640
import org.springframework.data.mapping.context.MappingContext;
@@ -59,6 +63,8 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
5963
@Getter private final NamedParameterJdbcOperations template;
6064
@Getter private SimpleTypeHolder simpleTypeHolder;
6165
private GenericConversionService conversions = getDefaultConversionService();
66+
@Setter
67+
private EntityInstantiators instantiators = new EntityInstantiators();
6268

6369
public JdbcMappingContext(NamingStrategy namingStrategy, NamedParameterJdbcOperations template,
6470
ConversionCustomizer customizer) {
@@ -130,6 +136,10 @@ public ConversionService getConversions() {
130136
return conversions;
131137
}
132138

139+
public EntityInstantiator getInstantiatorFor(PersistentEntity<?, ?> entity) {
140+
return instantiators.getInstantiatorFor(entity);
141+
}
142+
133143
private static GenericConversionService getDefaultConversionService() {
134144

135145
DefaultConversionService conversionService = new DefaultConversionService();

0 commit comments

Comments
 (0)