Skip to content
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

Unified RowMapper infrastructure #2000

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020-2025 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
*
* https://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.query;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Abstract {@link RowMapper} that delegates the actual mapping logic to a {@link AbstractDelegatingRowMapper#delegate delegate}
*
* @author Mikhail Polivakha
*/
public abstract class AbstractDelegatingRowMapper<T> implements RowMapper<T> {

private final RowMapper<T> delegate;

protected AbstractDelegatingRowMapper(RowMapper<T> delegate) {
Assert.notNull(delegate, "Delegating RowMapper cannot be null");

this.delegate = delegate;
}

@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
T intermediate = delegate.mapRow(rs, rowNum);
return postProcessMapping(intermediate);
}

/**
* The post-processing callback for implementations.
*
* @return the mapped entity after applying post-processing logic
*/
protected T postProcessMapping(@Nullable T object) {
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,60 +153,25 @@ private <T> JdbcQueryExecution<T> createSingleReadingQueryExecution(ResultSetExt
* Factory to create a {@link RowMapper} for a given class.
*
* @since 2.3
* @deprecated Use {@link org.springframework.data.jdbc.repository.query.RowMapperFactory} instead
*/
public interface RowMapperFactory {

/**
* Create a {@link RowMapper} based on the expected return type passed in as an argument.
*
* @param result must not be {@code null}.
* @return a {@code RowMapper} producing instances of {@code result}.
*/
RowMapper<Object> create(Class<?> result);

/**
* Obtain a {@code RowMapper} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
*
* @param reference must not be {@code null}.
* @since 3.4
*/
default RowMapper<Object> getRowMapper(String reference) {
throw new UnsupportedOperationException("getRowMapper is not supported");
}

/**
* Obtain a {@code ResultSetExtractor} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
*
* @param reference must not be {@code null}.
* @since 3.4
*/
default ResultSetExtractor<Object> getResultSetExtractor(String reference) {
throw new UnsupportedOperationException("getResultSetExtractor is not supported");
}
}
@Deprecated(forRemoval = true, since = "3.4.4")
public interface RowMapperFactory extends org.springframework.data.jdbc.repository.query.RowMapperFactory { }

/**
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterwards into {@code Object}.
*
* @param <T>
* @since 2.3
* @deprecated use {@link org.springframework.data.jdbc.repository.query.ConvertingRowMapper} instead
*/
protected static class ConvertingRowMapper<T> implements RowMapper<Object> {

private final RowMapper<T> delegate;
private final Converter<Object, Object> converter;
@Deprecated(forRemoval = true, since = "3.4.4")
protected static class ConvertingRowMapper<T> extends
org.springframework.data.jdbc.repository.query.ConvertingRowMapper {

@SuppressWarnings("unchecked")
public ConvertingRowMapper(RowMapper<T> delegate, Converter<Object, Object> converter) {
this.delegate = delegate;
this.converter = converter;
}

@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

T object = delegate.mapRow(rs, rowNum);

return object == null ? null : converter.convert(object);
super((RowMapper<Object>) delegate, converter);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020-2025 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
*
* https://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.query;

import java.sql.ResultSet;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.relational.core.mapping.event.AfterConvertCallback;
import org.springframework.data.relational.core.mapping.event.AfterConvertEvent;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;

/**
* Delegating {@link RowMapper} implementation that applies post-processing logic
* after the {@link RowMapper#mapRow(ResultSet, int)}. In particular, it emits the
* {@link AfterConvertEvent} event and invokes the {@link AfterConvertCallback} callbacks.
*
* @author Mark Paluch
* @author Mikhail Polivakha
*/
public class CallbackCapableRowMapper<T> extends AbstractDelegatingRowMapper<T> {

private final ApplicationEventPublisher publisher;
private final @Nullable EntityCallbacks callbacks;

public CallbackCapableRowMapper(RowMapper<T> delegate, ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks) {
super(delegate);
this.publisher = publisher;
this.callbacks = callbacks;
}

@Override
public T postProcessMapping(@Nullable T object) {
if (object != null) {

publisher.publishEvent(new AfterConvertEvent<>(object));

if (callbacks != null) {
return callbacks.callback(AfterConvertCallback.class, object);
}

}
return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2020-2025 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
*
* https://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.query;

import org.springframework.core.convert.converter.Converter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;

/**
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterwards into {@code Object}.
*
* @author Mark Paluch
* @author Mikhail Polivakha
*
* @since 2.3
*/
public class ConvertingRowMapper extends AbstractDelegatingRowMapper<Object> {

private final Converter<Object, Object> converter;

public ConvertingRowMapper(RowMapper<Object> delegate, Converter<Object, Object> converter) {
super(delegate);
this.converter = converter;
}

@Override
public Object postProcessMapping(@Nullable Object object) {
return object != null ? converter.convert(object) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2020-2025 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
*
* https://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.query;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.core.convert.EntityRowMapper;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;

/**
* Default implementation of {@link RowMapperFactory}. Honors the custom mappings defined
* in {@link QueryMappingConfiguration}.
* <p>
* This implementation is not capable of loading the {@link RowMapper} or {@link ResultSetExtractor}
* by reference via corresponding methods from {@link RowMapperFactory}.
*
* @implNote Public APIs of this class are thread-safe.
* @author Mikhail Polivakha
*/
public class DefaultRowMapperFactory implements RowMapperFactory {

private final RelationalMappingContext context;
private final JdbcConverter converter;
private final QueryMappingConfiguration queryMappingConfiguration;
private final EntityCallbacks entityCallbacks;
private final ApplicationEventPublisher publisher;

public DefaultRowMapperFactory(
RelationalMappingContext context,
JdbcConverter converter,
QueryMappingConfiguration queryMappingConfiguration,
EntityCallbacks entityCallbacks,
ApplicationEventPublisher publisher
) {
this.context = context;
this.converter = converter;
this.queryMappingConfiguration = queryMappingConfiguration;
this.entityCallbacks = entityCallbacks;
this.publisher = publisher;
}

@Override
@SuppressWarnings("unchecked")
public RowMapper<Object> create(Class<?> returnedObjectType) {

RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);

if (persistentEntity == null) {
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType,
converter.getConversionService());
}

return (RowMapper<Object>) determineDefaultMapper(returnedObjectType);
}

private RowMapper<?> determineDefaultMapper(Class<?> returnedObjectType) {

RowMapper<?> configuredQueryMapper = queryMappingConfiguration.getRowMapper(returnedObjectType);

if (configuredQueryMapper != null) {
return configuredQueryMapper;
}

EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( //
context.getRequiredPersistentEntity(returnedObjectType), //
converter //
);

return new CallbackCapableRowMapper<>(defaultEntityRowMapper, publisher, entityCallbacks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -98,7 +97,7 @@ public PartTreeJdbcQuery(RelationalMappingContext context, JdbcQueryMethod query
* @since 2.3
*/
public PartTreeJdbcQuery(RelationalMappingContext context, JdbcQueryMethod queryMethod, Dialect dialect,
JdbcConverter converter, NamedParameterJdbcOperations operations, RowMapperFactory rowMapperFactory) {
JdbcConverter converter, NamedParameterJdbcOperations operations, org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory) {

super(queryMethod, operations);

Expand Down Expand Up @@ -292,7 +291,7 @@ class CachedRowMapperFactory implements Supplier<RowMapper<?>> {
private final Lazy<RowMapper<?>> rowMapper;
private final Function<ResultProcessor, RowMapper<?>> rowMapperFunction;

public CachedRowMapperFactory(PartTree tree, RowMapperFactory rowMapperFactory, RelationalConverter converter,
public CachedRowMapperFactory(PartTree tree, org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, RelationalConverter converter,
ResultProcessor defaultResultProcessor) {

this.rowMapperFunction = processor -> {
Expand All @@ -302,7 +301,7 @@ public CachedRowMapperFactory(PartTree tree, RowMapperFactory rowMapperFactory,
}
Converter<Object, Object> resultProcessingConverter = new ResultProcessingConverter(processor,
converter.getMappingContext(), converter.getEntityInstantiators());
return new ConvertingRowMapper<>(rowMapperFactory.create(processor.getReturnedType().getDomainType()),
return new org.springframework.data.jdbc.repository.query.ConvertingRowMapper(rowMapperFactory.create(processor.getReturnedType().getDomainType()),
resultProcessingConverter);
};

Expand Down
Loading
Loading