From b32135fdd7a53e33ff4228bb3103a3e911c5d55c Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 5 Dec 2018 15:29:43 +0100 Subject: [PATCH] DATAJDBC-290 - Reestablish compatibility. Recreated RowMapperMap, its implementation and related methods in order to not to break existing implementations. Everything deprecated so we can remove it from 1.2 on. Original pull request: #101. See also: https://jira.spring.io/browse/DATAJDBC-302 --- .../repository/QueryMappingConfiguration.java | 4 +- .../data/jdbc/repository/RowMapperMap.java | 58 ++++++++++++++ .../config/ConfigurableRowMapperMap.java | 77 +++++++++++++++++++ .../DefaultQueryMappingConfiguration.java | 4 +- .../support/JdbcRepositoryFactory.java | 10 +++ .../support/JdbcRepositoryFactoryBean.java | 19 ++++- .../RowMapperOrResultsetExtractor.java | 4 +- 7 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/RowMapperMap.java create mode 100644 spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/ConfigurableRowMapperMap.java diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/QueryMappingConfiguration.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/QueryMappingConfiguration.java index 70c55f2b..ca58bc43 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/QueryMappingConfiguration.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/QueryMappingConfiguration.java @@ -14,7 +14,7 @@ import org.springframework.lang.Nullable; public interface QueryMappingConfiguration { @Nullable - RowMapperOrResultsetExtractor getMapperOrExtractor(Class type); + RowMapperOrResultsetExtractor getMapperOrExtractor(Class type); /** * An immutable empty instance that will return {@literal null} for all arguments. @@ -22,7 +22,7 @@ public interface QueryMappingConfiguration { QueryMappingConfiguration EMPTY = new QueryMappingConfiguration() { @Override - public RowMapperOrResultsetExtractor getMapperOrExtractor(Class type) { + public RowMapperOrResultsetExtractor getMapperOrExtractor(Class type) { return null; } diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/RowMapperMap.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/RowMapperMap.java new file mode 100644 index 00000000..56a9f216 --- /dev/null +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/RowMapperMap.java @@ -0,0 +1,58 @@ +/* + * Copyright 2018 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.repository; + +import org.springframework.data.jdbc.support.RowMapperOrResultsetExtractor; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.lang.Nullable; + +/** + * A map from a type to a {@link RowMapper} to be used for extracting that type from {@link java.sql.ResultSet}s. + * + * @author Jens Schauder + * @deprecated use {@link QueryMappingConfiguration} + */ +@Deprecated +public interface RowMapperMap extends QueryMappingConfiguration { + + /** + * An immutable empty instance that will return {@literal null} for all arguments. + */ + RowMapperMap EMPTY = new RowMapperMap() { + + /* + * (non-Javadoc) + * @see org.springframework.data.jdbc.repository.RowMapperMap#rowMapperFor(java.lang.Class) + */ + public RowMapper rowMapperFor(Class type) { + return null; + } + + @Override + public RowMapperOrResultsetExtractor getMapperOrExtractor(Class type) { + return null; + } + }; + + @Nullable + RowMapper rowMapperFor(Class type); + + + @Override + default RowMapperOrResultsetExtractor getMapperOrExtractor(Class type) { + return RowMapperOrResultsetExtractor.of(rowMapperFor(type)); + } +} diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/ConfigurableRowMapperMap.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/ConfigurableRowMapperMap.java new file mode 100644 index 00000000..0ad537c6 --- /dev/null +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/ConfigurableRowMapperMap.java @@ -0,0 +1,77 @@ +/* + * Copyright 2018 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.repository.config; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.springframework.data.jdbc.repository.RowMapperMap; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; + +/** + * A {@link RowMapperMap} that allows for registration of {@link RowMapper}s via a fluent Api. + * + * @author Jens Schauder + * @deprecated Use {@link DefaultQueryMappingConfiguration} instead. + */ +@Deprecated +public class ConfigurableRowMapperMap implements RowMapperMap { + + private Map, RowMapper> rowMappers = new LinkedHashMap<>(); + + /** + * Registers a the given {@link RowMapper} as to be used for the given type. + * + * @return this instance, so this can be used as a fluent interface. + */ + public ConfigurableRowMapperMap register(Class type, RowMapper rowMapper) { + + rowMappers.put(type, rowMapper); + return this; + } + + /** + * Returs a {@link RowMapper} for the given type if such a {@link RowMapper} is present. If an exact match is found + * that is returned. If not a {@link RowMapper} is returned that produces subtypes of the requested type. If no such + * {@link RowMapper} is found the method returns {@code null}. + * + * @param type the type to be produced by the returned {@link RowMapper}. Must not be {@code null}. + * @param the type to be produced by the returned {@link RowMapper}. + * @return Guaranteed to be not {@code null}. + */ + @SuppressWarnings("unchecked") + @Nullable + public RowMapper rowMapperFor(Class type) { + + Assert.notNull(type, "Type must not be null"); + + RowMapper candidate = (RowMapper) rowMappers.get(type); + + if (candidate == null) { + + for (Map.Entry, RowMapper> entry : rowMappers.entrySet()) { + + if (type.isAssignableFrom(entry.getKey())) { + candidate = (RowMapper) entry.getValue(); + } + } + } + + return candidate; + } +} diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/DefaultQueryMappingConfiguration.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/DefaultQueryMappingConfiguration.java index 3c53250a..2749dc08 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/DefaultQueryMappingConfiguration.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/DefaultQueryMappingConfiguration.java @@ -22,7 +22,7 @@ public class DefaultQueryMappingConfiguration implements QueryMappingConfigurati private Map, RowMapperOrResultsetExtractor> mappers = new LinkedHashMap<>(); @Nullable - public RowMapperOrResultsetExtractor getMapperOrExtractor(Class type) { + public RowMapperOrResultsetExtractor getMapperOrExtractor(Class type) { Assert.notNull(type, "Type must not be null"); @@ -37,7 +37,7 @@ public class DefaultQueryMappingConfiguration implements QueryMappingConfigurati } } } - return candidate; + return (RowMapperOrResultsetExtractor) candidate; } /** diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java index d9192617..ec583b1a 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.java @@ -21,6 +21,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.jdbc.core.DataAccessStrategy; import org.springframework.data.jdbc.core.JdbcAggregateTemplate; import org.springframework.data.jdbc.repository.QueryMappingConfiguration; +import org.springframework.data.jdbc.repository.RowMapperMap; import org.springframework.data.relational.core.conversion.RelationalConverter; import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; @@ -89,6 +90,15 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport { this.queryMappingConfiguration = queryMappingConfiguration; } + /** + * @param rowMapperMap must not be {@literal null} consider {@link RowMapperMap#EMPTY} instead. + * @deprecated use {@link #setQueryMappingConfiguration(QueryMappingConfiguration)} instead + */ + @Deprecated + public void setRowMapperMap(RowMapperMap rowMapperMap) { + setQueryMappingConfiguration(rowMapperMap); + } + @SuppressWarnings("unchecked") @Override public EntityInformation getEntityInformation(Class aClass) { diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java index de182f22..58c16568 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java @@ -24,6 +24,7 @@ import org.springframework.data.jdbc.core.DataAccessStrategy; import org.springframework.data.jdbc.core.DefaultDataAccessStrategy; import org.springframework.data.jdbc.core.SqlGeneratorSource; import org.springframework.data.jdbc.repository.QueryMappingConfiguration; +import org.springframework.data.jdbc.repository.RowMapperMap; import org.springframework.data.relational.core.conversion.RelationalConverter; import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.repository.Repository; @@ -102,12 +103,24 @@ public class JdbcRepositoryFactoryBean, S, ID extend } /** - * @param rowMapperMap can be {@literal null}. {@link #afterPropertiesSet()} defaults to + * @param queryMappingConfiguration can be {@literal null}. {@link #afterPropertiesSet()} defaults to * {@link QueryMappingConfiguration#EMPTY} if {@literal null}. */ @Autowired(required = false) - public void setQueryMappingConfiguration(QueryMappingConfiguration rowMapperMap) { - this.queryMappingConfiguration = rowMapperMap; + public void setQueryMappingConfiguration(QueryMappingConfiguration queryMappingConfiguration) { + this.queryMappingConfiguration = queryMappingConfiguration; + } + + /** + * @param rowMapperMap can be {@literal null}. {@link #afterPropertiesSet()} defaults to {@link RowMapperMap#EMPTY} if + * {@literal null}. + * + * @deprecated use {@link #setQueryMappingConfiguration(QueryMappingConfiguration)} instead. + */ + @Deprecated + @Autowired(required = false) + public void setRowMapperMap(RowMapperMap rowMapperMap) { + setQueryMappingConfiguration(rowMapperMap); } @Autowired diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/support/RowMapperOrResultsetExtractor.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/support/RowMapperOrResultsetExtractor.java index 9506119c..aeea47d3 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/support/RowMapperOrResultsetExtractor.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/support/RowMapperOrResultsetExtractor.java @@ -23,11 +23,11 @@ public class RowMapperOrResultsetExtractor { this.resultSetExtractor = resultSetExtractor; } - public static RowMapperOrResultsetExtractor of(RowMapper rowMapper) { + public static RowMapperOrResultsetExtractor of(RowMapper rowMapper) { return new RowMapperOrResultsetExtractor<>(rowMapper, null); } - public static RowMapperOrResultsetExtractor of(ResultSetExtractor resultSetExtractor) { + public static RowMapperOrResultsetExtractor of(ResultSetExtractor resultSetExtractor) { return new RowMapperOrResultsetExtractor<>(null, resultSetExtractor); }