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
This commit is contained in:
Jens Schauder
2018-12-05 15:29:43 +01:00
parent 3769db7a27
commit b32135fdd7
7 changed files with 167 additions and 9 deletions

View File

@@ -14,7 +14,7 @@ import org.springframework.lang.Nullable;
public interface QueryMappingConfiguration {
@Nullable
<T> RowMapperOrResultsetExtractor<?> getMapperOrExtractor(Class<T> type);
<T> RowMapperOrResultsetExtractor<? extends T> getMapperOrExtractor(Class<T> 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 <T> RowMapperOrResultsetExtractor<?> getMapperOrExtractor(Class<T> type) {
public <T> RowMapperOrResultsetExtractor<? extends T> getMapperOrExtractor(Class<T> type) {
return null;
}

View File

@@ -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 <T> RowMapper<? extends T> rowMapperFor(Class<T> type) {
return null;
}
@Override
public <T> RowMapperOrResultsetExtractor<T> getMapperOrExtractor(Class<T> type) {
return null;
}
};
@Nullable
<T> RowMapper<? extends T> rowMapperFor(Class<T> type);
@Override
default <T> RowMapperOrResultsetExtractor<? extends T> getMapperOrExtractor(Class<T> type) {
return RowMapperOrResultsetExtractor.of(rowMapperFor(type));
}
}

View File

@@ -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<Class<?>, 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 <T> ConfigurableRowMapperMap register(Class<T> type, RowMapper<? extends T> 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 <T> the type to be produced by the returned {@link RowMapper}.
* @return Guaranteed to be not {@code null}.
*/
@SuppressWarnings("unchecked")
@Nullable
public <T> RowMapper<? extends T> rowMapperFor(Class<T> type) {
Assert.notNull(type, "Type must not be null");
RowMapper<? extends T> candidate = (RowMapper<? extends T>) rowMappers.get(type);
if (candidate == null) {
for (Map.Entry<Class<?>, RowMapper<?>> entry : rowMappers.entrySet()) {
if (type.isAssignableFrom(entry.getKey())) {
candidate = (RowMapper<? extends T>) entry.getValue();
}
}
}
return candidate;
}
}

View File

@@ -22,7 +22,7 @@ public class DefaultQueryMappingConfiguration implements QueryMappingConfigurati
private Map<Class<?>, RowMapperOrResultsetExtractor<?>> mappers = new LinkedHashMap<>();
@Nullable
public <T> RowMapperOrResultsetExtractor<?> getMapperOrExtractor(Class<T> type) {
public <T> RowMapperOrResultsetExtractor<? extends T> getMapperOrExtractor(Class<T> type) {
Assert.notNull(type, "Type must not be null");
@@ -37,7 +37,7 @@ public class DefaultQueryMappingConfiguration implements QueryMappingConfigurati
}
}
}
return candidate;
return (RowMapperOrResultsetExtractor<? extends T>) candidate;
}
/**

View File

@@ -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 <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {

View File

@@ -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<T extends Repository<S, ID>, 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

View File

@@ -23,11 +23,11 @@ public class RowMapperOrResultsetExtractor<T> {
this.resultSetExtractor = resultSetExtractor;
}
public static RowMapperOrResultsetExtractor<?> of(RowMapper<?> rowMapper) {
public static <T> RowMapperOrResultsetExtractor<T> of(RowMapper<T> rowMapper) {
return new RowMapperOrResultsetExtractor<>(rowMapper, null);
}
public static RowMapperOrResultsetExtractor<?> of(ResultSetExtractor<?> resultSetExtractor) {
public static <T> RowMapperOrResultsetExtractor<T> of(ResultSetExtractor<T> resultSetExtractor) {
return new RowMapperOrResultsetExtractor<>(null, resultSetExtractor);
}