Skip to content

DATAJDBC-147 - Support registration von converters via ConversionCustomizers. #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<version>1.0.0.DATAJDBC-147-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.NonTransientDataAccessException;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
Expand Down Expand Up @@ -58,7 +54,6 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
private final SqlGeneratorSource sqlGeneratorSource;
private final NamedParameterJdbcOperations operations;
private final JdbcMappingContext context;
private final ConversionService conversions = getDefaultConversionService();
private final DataAccessStrategy accessStrategy;

public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, NamedParameterJdbcOperations operations,
Expand Down Expand Up @@ -215,14 +210,6 @@ public <T> boolean existsById(Object id, Class<T> domainType) {
return operations.queryForObject(existsSql, parameter, Boolean.class);
}

private static GenericConversionService getDefaultConversionService() {

DefaultConversionService conversionService = new DefaultConversionService();
Jsr310Converters.getConvertersToRegister().forEach(conversionService::addConverter);

return conversionService;
}

private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersistentEntity<S> persistentEntity) {

MapSqlParameterSource parameters = new MapSqlParameterSource();
Expand Down Expand Up @@ -288,7 +275,7 @@ private <S> Optional<Object> getIdFromHolder(KeyHolder holder, JdbcPersistentEnt
}

private <T> EntityRowMapper<T> getEntityRowMapper(Class<T> domainType) {
return new EntityRowMapper<>(getRequiredPersistentEntity(domainType), conversions, context, accessStrategy);
return new EntityRowMapper<>(getRequiredPersistentEntity(domainType), context.getConversions(), context, accessStrategy);
}

private RowMapper getMapEntityRowMapper(JdbcPersistentProperty property) {
Expand Down Expand Up @@ -316,7 +303,7 @@ private <V> V convert(Object from, Class<V> to) {

Object id = persistentEntity == null ? null : persistentEntity.getIdentifierAccessor(from).getIdentifier();

return conversions.convert(id == null ? from : id, to);
return context.getConversions().convert(id == null ? from : id, to);
}

private SqlGenerator sql(Class<?> domainType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.mapping.model;

import org.springframework.core.convert.support.GenericConversionService;

/**
* @author Jens Schauder
*/
public interface ConversionCustomizer {

void customize(GenericConversionService conversions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import java.util.HashSet;
import java.util.List;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.context.AbstractMappingContext;
import org.springframework.data.mapping.context.MappingContext;
Expand All @@ -50,13 +54,21 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
));

private final @Getter NamingStrategy namingStrategy;
private GenericConversionService conversions = getDefaultConversionService();

public JdbcMappingContext(NamingStrategy namingStrategy) {
public JdbcMappingContext(NamingStrategy namingStrategy, ConversionCustomizer customizer) {

this.namingStrategy = namingStrategy;

customizer.customize(conversions);

setSimpleTypeHolder(new SimpleTypeHolder(CUSTOM_SIMPLE_TYPES, true));
}

public JdbcMappingContext() {
this(new DefaultNamingStrategy(), __ -> {});
}

public List<PropertyPath> referencedEntities(Class<?> rootType, PropertyPath path) {

List<PropertyPath> paths = new ArrayList<>();
Expand Down Expand Up @@ -103,4 +115,16 @@ protected JdbcPersistentProperty createPersistentProperty(Property property, Jdb
return new BasicJdbcPersistentEntityInformation<>((JdbcPersistentEntity<T>) getRequiredPersistentEntity(type));
}

public ConversionService getConversions() {
return conversions;
}

private static GenericConversionService getDefaultConversionService() {

DefaultConversionService conversionService = new DefaultConversionService();
Jsr310Converters.getConvertersToRegister().forEach(conversionService::addConverter);

return conversionService;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.DelegatingDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.mapping.model.ConversionCustomizer;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
Expand Down Expand Up @@ -65,22 +66,23 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
private static final String DATA_SOURCE_BEAN_NAME = "dataSource";
private static final String NAMING_STRATEGY_BEAN_NAME = "namingStrategy";
private static final String SQL_SESSION_FACTORY_BEAN_NAME = "sqlSessionFactory";
private static final String CONVERSION_CUSTOMIZER_BEAN_NAME = "conversionCustomizer";

private final ApplicationEventPublisher applicationEventPublisher;
private final ApplicationContext context;
private final ApplicationContext applicationContext;

JdbcRepositoryFactoryBean(Class<? extends T> repositoryInterface, ApplicationEventPublisher applicationEventPublisher,
ApplicationContext context) {
ApplicationContext applicationContext) {

super(repositoryInterface);
this.applicationEventPublisher = applicationEventPublisher;
this.context = context;
this.applicationContext = applicationContext;
}

@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {

final JdbcMappingContext context = new JdbcMappingContext(findOrCreateNamingStrategy());
final JdbcMappingContext context = new JdbcMappingContext(findOrCreateNamingStrategy(), findOrCreateConversionCustomizer());

return new JdbcRepositoryFactory(applicationEventPublisher, context, createDataAccessStrategy(context));
}
Expand Down Expand Up @@ -156,6 +158,10 @@ private NamingStrategy findOrCreateNamingStrategy() {
return getNamingStrategy().orElse(new DefaultNamingStrategy());
}

private ConversionCustomizer findOrCreateConversionCustomizer() {
return getConversionCustomizer().orElse(conversionService->{});
}

private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() {
return getBean(NamedParameterJdbcOperations.class, NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME);
}
Expand All @@ -172,9 +178,13 @@ private Optional<NamingStrategy> getNamingStrategy() {
return getBean(NamingStrategy.class, NAMING_STRATEGY_BEAN_NAME);
}

private Optional<ConversionCustomizer> getConversionCustomizer() {
return getBean(ConversionCustomizer.class, CONVERSION_CUSTOMIZER_BEAN_NAME);
}

private <R> Optional<R> getBean(Class<R> type, String name) {

Map<String, R> beansOfType = context.getBeansOfType(type);
Map<String, R> beansOfType = applicationContext.getBeansOfType(type);

if (beansOfType.size() == 1) {
return beansOfType.values().stream().findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty;
Expand Down Expand Up @@ -118,7 +117,7 @@ public void mapReferenceGetsLoadedWithAdditionalSelect() throws SQLException {

private <T> EntityRowMapper<T> createRowMapper(Class<T> type) {

JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy());
JdbcMappingContext context = new JdbcMappingContext();
DataAccessStrategy accessStrategy = mock(DataAccessStrategy.class);

// the ID of the entity is used to determin what kind of resultset is needed for subsequent selects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
Expand Down Expand Up @@ -254,7 +253,7 @@ Class<?> testClass() {
JdbcEntityOperations operations(ApplicationEventPublisher publisher,
NamedParameterJdbcOperations namedParameterJdbcOperations) {

final JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy());
final JdbcMappingContext context = new JdbcMappingContext();
return new JdbcEntityTemplate(publisher, context, dataAccessStrategy(namedParameterJdbcOperations, context));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private void threadedTest(String user, CountDownLatch latch, Consumer<String> te
*/
private SqlGenerator configureSqlGenerator(NamingStrategy namingStrategy) {

JdbcMappingContext context = new JdbcMappingContext(namingStrategy);
JdbcMappingContext context = new JdbcMappingContext(namingStrategy, __ -> {});
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);

return new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void deleteByList() {
*/
private SqlGenerator configureSqlGenerator(NamingStrategy namingStrategy) {

JdbcMappingContext context = new JdbcMappingContext(namingStrategy);
JdbcMappingContext context = new JdbcMappingContext(namingStrategy, __ -> {});
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
return new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SqlGeneratorUnitTests {
public void setUp() {

NamingStrategy namingStrategy = new PrefixingNamingStrategy();
JdbcMappingContext context = new JdbcMappingContext(namingStrategy);
JdbcMappingContext context = new JdbcMappingContext(namingStrategy, __ -> {});
JdbcPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
this.sqlGenerator = new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.conversion.AggregateChange.Kind;
import org.springframework.data.jdbc.core.conversion.DbAction.Delete;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;

/**
Expand All @@ -36,7 +35,7 @@
@RunWith(MockitoJUnitRunner.class)
public class JdbcEntityDeleteWriterUnitTests {

JdbcEntityDeleteWriter converter = new JdbcEntityDeleteWriter(new JdbcMappingContext(new DefaultNamingStrategy()));
JdbcEntityDeleteWriter converter = new JdbcEntityDeleteWriter(new JdbcMappingContext());

@Test
public void deleteDeletesTheEntityAndReferencedEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.springframework.data.jdbc.core.conversion.DbAction.Delete;
import org.springframework.data.jdbc.core.conversion.DbAction.Insert;
import org.springframework.data.jdbc.core.conversion.DbAction.Update;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;

/**
Expand All @@ -44,7 +43,7 @@
public class JdbcEntityWriterUnitTests {

public static final long SOME_ENTITY_ID = 23L;
JdbcEntityWriter converter = new JdbcEntityWriter(new JdbcMappingContext(new DefaultNamingStrategy()));
JdbcEntityWriter converter = new JdbcEntityWriter(new JdbcMappingContext());

@Test // DATAJDBC-112
public void newEntityGetsConvertedToOneInsert() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class BasicJdbcPersistentPropertyUnitTests {
@Test // DATAJDBC-104
public void enumGetsStoredAsString() {

JdbcPersistentEntity<?> persistentEntity = new JdbcMappingContext(new DefaultNamingStrategy())
JdbcPersistentEntity<?> persistentEntity = new JdbcMappingContext()
.getRequiredPersistentEntity(DummyEntity.class);

persistentEntity.doWithProperties((PropertyHandler<JdbcPersistentProperty>) p -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.springframework.data.jdbc.mapping.event.BeforeSave;
import org.springframework.data.jdbc.mapping.event.Identifier;
import org.springframework.data.jdbc.mapping.event.JdbcEvent;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.repository.CrudRepository;
Expand All @@ -47,7 +46,7 @@ public class SimpleJdbcRepositoryEventsUnitTests {
@Before
public void before() {

final JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy());
final JdbcMappingContext context = new JdbcMappingContext();
JdbcRepositoryFactory factory = new JdbcRepositoryFactory( //
publisher, //
context, //
Expand Down
Loading