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.

Original pull request: #68.
This commit is contained in:
Jens Schauder
2018-04-18 13:04:17 +02:00
committed by Mark Paluch
parent 17476391fe
commit 0ebb4a7799
11 changed files with 106 additions and 42 deletions

View File

@@ -16,7 +16,8 @@
package org.springframework.data.jdbc.core;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import lombok.RequiredArgsConstructor;
@@ -26,8 +27,8 @@ import java.util.HashMap;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.core.mapping.NamingStrategy;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.KeyHolder;
@@ -47,8 +48,12 @@ public class DefaultDataAccessStrategyUnitTests {
HashMap<String, Object> additionalParameters = new HashMap<>();
ArgumentCaptor<SqlParameterSource> paramSourceCaptor = ArgumentCaptor.forClass(SqlParameterSource.class);
DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy(new SqlGeneratorSource(context), context,
jdbcOperations);
DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy( //
new SqlGeneratorSource(context), //
context, //
jdbcOperations, //
new EntityInstantiators() //
);
@Test // DATAJDBC-146
public void additionalParameterForIdDoesNotLeadToDuplicateParameters() {

View File

@@ -17,7 +17,8 @@ package org.springframework.data.jdbc.core;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import lombok.RequiredArgsConstructor;
@@ -40,12 +41,12 @@ import org.mockito.stubbing.Answer;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.core.mapping.JdbcPersistentEntity;
import org.springframework.data.jdbc.core.mapping.JdbcPersistentProperty;
import org.springframework.data.jdbc.core.mapping.NamingStrategy;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.util.Assert;
/**
@@ -199,8 +200,12 @@ public class EntityRowMapperUnitTests {
DefaultConversionService.addDefaultConverters(conversionService);
Jsr310Converters.getConvertersToRegister().forEach(conversionService::addConverter);
return new EntityRowMapper<>((JdbcPersistentEntity<T>) context.getRequiredPersistentEntity(type), context,
accessStrategy);
return new EntityRowMapper<>( //
(JdbcPersistentEntity<T>) context.getRequiredPersistentEntity(type), //
context, //
new EntityInstantiators(), //
accessStrategy //
);
}
private static ResultSet mockResultSet(List<String> columns, Object... values) {
@@ -212,7 +217,7 @@ public class EntityRowMapperUnitTests {
"Number of values [%d] must be a multiple of the number of columns [%d]", //
values.length, //
columns.size() //
) //
) //
);
List<Map<String, Object>> result = convertValues(columns, values);

View File

@@ -17,7 +17,9 @@ package org.springframework.data.jdbc.repository;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import junit.framework.AssertionFailedError;
@@ -34,6 +36,7 @@ import org.junit.Test;
import org.mockito.stubbing.Answer;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
@@ -73,7 +76,8 @@ public class SimpleJdbcRepositoryEventsUnitTests {
NamedParameterJdbcOperations operations = createIdGeneratingOperations();
SqlGeneratorSource generatorSource = new SqlGeneratorSource(context);
this.dataAccessStrategy = spy(new DefaultDataAccessStrategy(generatorSource, context, operations));
this.dataAccessStrategy = spy(
new DefaultDataAccessStrategy(generatorSource, context, operations, new EntityInstantiators()));
JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, publisher, operations);
@@ -93,7 +97,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.containsExactly( //
BeforeSaveEvent.class, //
AfterSaveEvent.class //
);
);
}
@Test // DATAJDBC-99
@@ -112,7 +116,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
AfterSaveEvent.class, //
BeforeSaveEvent.class, //
AfterSaveEvent.class //
);
);
}
@Test // DATAJDBC-99
@@ -143,7 +147,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.containsExactly( //
BeforeDeleteEvent.class, //
AfterDeleteEvent.class //
);
);
}
@Test // DATAJDBC-197
@@ -162,7 +166,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.containsExactly( //
AfterLoadEvent.class, //
AfterLoadEvent.class //
);
);
}
@Test // DATAJDBC-197
@@ -181,7 +185,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.containsExactly( //
AfterLoadEvent.class, //
AfterLoadEvent.class //
);
);
}
@Test // DATAJDBC-197
@@ -198,7 +202,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
AfterLoadEvent.class //
);
);
}
private static NamedParameterJdbcOperations createIdGeneratingOperations() {

View File

@@ -15,7 +15,9 @@
*/
package org.springframework.data.jdbc.repository.support;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
@@ -23,6 +25,7 @@ import java.text.NumberFormat;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.repository.RowMapperMap;
@@ -76,8 +79,8 @@ public class JdbcQueryLookupStrategyUnitTests {
private RepositoryQuery getRepositoryQuery(String name, RowMapperMap rowMapperMap) {
JdbcQueryLookupStrategy queryLookupStrategy = new JdbcQueryLookupStrategy(mappingContext, accessStrategy,
rowMapperMap, operations);
JdbcQueryLookupStrategy queryLookupStrategy = new JdbcQueryLookupStrategy(mappingContext, new EntityInstantiators(),
accessStrategy, rowMapperMap, operations);
return queryLookupStrategy.resolveQuery(getMethod(name), metadata, projectionFactory, namedQueries);
}

View File

@@ -25,6 +25,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
@@ -71,7 +72,7 @@ public class TestConfiguration {
@Bean
DataAccessStrategy defaultDataAccessStrategy(JdbcMappingContext context) {
return new DefaultDataAccessStrategy(new SqlGeneratorSource(context), context, namedParameterJdbcTemplate());
return new DefaultDataAccessStrategy(new SqlGeneratorSource(context), context, namedParameterJdbcTemplate(), new EntityInstantiators());
}
@Bean