Browse Source

Polishing.

Formatting.

Original pull request #2000
See #1998
pull/2065/head
Jens Schauder 7 months ago committed by Mark Paluch
parent
commit
5d3d38e8ef
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 10
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractDelegatingRowMapper.java
  2. 8
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java
  3. 16
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/CallbackCapableRowMapper.java
  4. 10
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/ConvertingRowMapper.java
  5. 21
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/DefaultRowMapperFactory.java
  6. 14
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java
  7. 13
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/RowMapperFactory.java
  8. 6
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQuery.java
  9. 28
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java

10
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractDelegatingRowMapper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2020-2025 the original author or authors.
* Copyright 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.
@ -23,22 +23,27 @@ import org.springframework.lang.Nullable; @@ -23,22 +23,27 @@ 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}
* Abstract {@link RowMapper} that delegates the actual mapping logic to a {@link AbstractDelegatingRowMapper#delegate
* delegate}
*
* @author Mikhail Polivakha
* @since 4.0
*/
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
@Nullable
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
T intermediate = delegate.mapRow(rs, rowNum);
return postProcessMapping(intermediate);
}
@ -48,6 +53,7 @@ public abstract class AbstractDelegatingRowMapper<T> implements RowMapper<T> { @@ -48,6 +53,7 @@ public abstract class AbstractDelegatingRowMapper<T> implements RowMapper<T> {
*
* @return the mapped entity after applying post-processing logic
*/
@Nullable
protected T postProcessMapping(@Nullable T object) {
return object;
}

8
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java

@ -15,8 +15,6 @@ @@ -15,8 +15,6 @@
*/
package org.springframework.data.jdbc.repository.query;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
@ -156,7 +154,7 @@ public abstract class AbstractJdbcQuery implements RepositoryQuery { @@ -156,7 +154,7 @@ public abstract class AbstractJdbcQuery implements RepositoryQuery {
* @deprecated Use {@link org.springframework.data.jdbc.repository.query.RowMapperFactory} instead
*/
@Deprecated(forRemoval = true, since = "3.4.4")
public interface RowMapperFactory extends org.springframework.data.jdbc.repository.query.RowMapperFactory { }
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}.
@ -166,8 +164,8 @@ public abstract class AbstractJdbcQuery implements RepositoryQuery { @@ -166,8 +164,8 @@ public abstract class AbstractJdbcQuery implements RepositoryQuery {
* @deprecated use {@link org.springframework.data.jdbc.repository.query.ConvertingRowMapper} instead
*/
@Deprecated(forRemoval = true, since = "3.4.4")
protected static class ConvertingRowMapper<T> extends
org.springframework.data.jdbc.repository.query.ConvertingRowMapper {
protected static class ConvertingRowMapper<T>
extends org.springframework.data.jdbc.repository.query.ConvertingRowMapper {
@SuppressWarnings("unchecked")
public ConvertingRowMapper(RowMapper<T> delegate, Converter<Object, Object> converter) {

16
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/CallbackCapableRowMapper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2020-2025 the original author or authors.
* Copyright 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.
@ -25,26 +25,32 @@ import org.springframework.jdbc.core.RowMapper; @@ -25,26 +25,32 @@ 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.
* 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
* @since 4.0
*/
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) {
public CallbackCapableRowMapper(RowMapper<T> delegate, ApplicationEventPublisher publisher,
@Nullable EntityCallbacks callbacks) {
super(delegate);
this.publisher = publisher;
this.callbacks = callbacks;
}
@Override
@Nullable
public T postProcessMapping(@Nullable T object) {
if (object != null) {
publisher.publishEvent(new AfterConvertEvent<>(object));

10
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/ConvertingRowMapper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2020-2025 the original author or authors.
* Copyright 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.
@ -20,23 +20,25 @@ import org.springframework.jdbc.core.RowMapper; @@ -20,23 +20,25 @@ 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}.
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterward into {@code Object}.
*
* @author Mark Paluch
* @author Mikhail Polivakha
*
* @since 2.3
* @since 4.0
*/
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
@Nullable
public Object postProcessMapping(@Nullable Object object) {
return object != null ? converter.convert(object) : null;
}

21
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/DefaultRowMapperFactory.java

@ -27,14 +27,15 @@ import org.springframework.jdbc.core.RowMapper; @@ -27,14 +27,15 @@ 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}.
* 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}.
* 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
* @since 4.0
*/
public class DefaultRowMapperFactory implements RowMapperFactory {
@ -44,13 +45,10 @@ public class DefaultRowMapperFactory implements RowMapperFactory { @@ -44,13 +45,10 @@ public class DefaultRowMapperFactory implements RowMapperFactory {
private final EntityCallbacks entityCallbacks;
private final ApplicationEventPublisher publisher;
public DefaultRowMapperFactory(
RelationalMappingContext context,
JdbcConverter converter,
QueryMappingConfiguration queryMappingConfiguration,
EntityCallbacks entityCallbacks,
ApplicationEventPublisher publisher
) {
public DefaultRowMapperFactory(RelationalMappingContext context, JdbcConverter converter,
QueryMappingConfiguration queryMappingConfiguration, EntityCallbacks entityCallbacks,
ApplicationEventPublisher publisher) {
this.context = context;
this.converter = converter;
this.queryMappingConfiguration = queryMappingConfiguration;
@ -65,6 +63,7 @@ public class DefaultRowMapperFactory implements RowMapperFactory { @@ -65,6 +63,7 @@ public class DefaultRowMapperFactory implements RowMapperFactory {
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);
if (persistentEntity == null) {
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType,
converter.getConversionService());
}

14
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQuery.java

@ -97,7 +97,8 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery { @@ -97,7 +97,8 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery {
* @since 2.3
*/
public PartTreeJdbcQuery(RelationalMappingContext context, JdbcQueryMethod queryMethod, Dialect dialect,
JdbcConverter converter, NamedParameterJdbcOperations operations, org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory) {
JdbcConverter converter, NamedParameterJdbcOperations operations,
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory) {
super(queryMethod, operations);
@ -160,13 +161,13 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery { @@ -160,13 +161,13 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery {
JdbcQueryExecution<?> queryExecution = getJdbcQueryExecution(extractor, rowMapper);
if (getQueryMethod().isSliceQuery()) {
//noinspection unchecked
// noinspection unchecked
return new SliceQueryExecution<>((JdbcQueryExecution<Collection<Object>>) queryExecution, accessor.getPageable());
}
if (getQueryMethod().isPageQuery()) {
//noinspection unchecked
// noinspection unchecked
return new PageQueryExecution<>((JdbcQueryExecution<Collection<Object>>) queryExecution, accessor.getPageable(),
() -> {
@ -291,7 +292,8 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery { @@ -291,7 +292,8 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery {
private final Lazy<RowMapper<?>> rowMapper;
private final Function<ResultProcessor, RowMapper<?>> rowMapperFunction;
public CachedRowMapperFactory(PartTree tree, org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, RelationalConverter converter,
public CachedRowMapperFactory(PartTree tree,
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, RelationalConverter converter,
ResultProcessor defaultResultProcessor) {
this.rowMapperFunction = processor -> {
@ -301,8 +303,8 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery { @@ -301,8 +303,8 @@ public class PartTreeJdbcQuery extends AbstractJdbcQuery {
}
Converter<Object, Object> resultProcessingConverter = new ResultProcessingConverter(processor,
converter.getMappingContext(), converter.getEntityInstantiators());
return new org.springframework.data.jdbc.repository.query.ConvertingRowMapper(rowMapperFactory.create(processor.getReturnedType().getDomainType()),
resultProcessingConverter);
return new org.springframework.data.jdbc.repository.query.ConvertingRowMapper(
rowMapperFactory.create(processor.getReturnedType().getDomainType()), resultProcessingConverter);
};
this.rowMapper = Lazy.of(() -> this.rowMapperFunction.apply(defaultResultProcessor));

13
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/RowMapperFactory.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2020-2025 the original author or authors.
* Copyright 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.
@ -23,8 +23,7 @@ import org.springframework.jdbc.core.RowMapper; @@ -23,8 +23,7 @@ import org.springframework.jdbc.core.RowMapper;
*
* @author Jens Schauder
* @author Mikhail Polivakha
*
* @since 2.3
* @since 4.0
*/
public interface RowMapperFactory {
@ -37,20 +36,20 @@ public interface RowMapperFactory { @@ -37,20 +36,20 @@ public interface RowMapperFactory {
RowMapper<Object> create(Class<?> result);
/**
* Obtain a {@link RowMapper} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
* Obtain a {@link 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 by reference is not supported");
}
/**
* Obtain a {@code ResultSetExtractor} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
* 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 by reference is not supported");

6
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQuery.java

@ -95,7 +95,8 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery { @@ -95,7 +95,8 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery {
* @since 3.4
*/
public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, JdbcConverter converter, ValueExpressionDelegate delegate) {
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, JdbcConverter converter,
ValueExpressionDelegate delegate) {
this(queryMethod.getRequiredQuery(), queryMethod, operations, rowMapperFactory, converter, delegate);
}
@ -112,7 +113,8 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery { @@ -112,7 +113,8 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery {
* @since 3.4
*/
public StringBasedJdbcQuery(String query, JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, JdbcConverter converter, ValueExpressionDelegate delegate) {
org.springframework.data.jdbc.repository.query.RowMapperFactory rowMapperFactory, JdbcConverter converter,
ValueExpressionDelegate delegate) {
super(queryMethod, operations);
Assert.hasText(query, "Query must not be null or empty");
Assert.notNull(rowMapperFactory, "RowMapperFactory must not be null");

28
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java

@ -109,10 +109,10 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy { @@ -109,10 +109,10 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
ValueExpressionDelegate delegate) {
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
delegate);
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, delegate);
this.rowMapperFactory = new DefaultRowMapperFactory(getMappingContext(), getConverter(), getQueryMappingConfiguration(), getCallbacks(), getPublisher());
this.rowMapperFactory = new DefaultRowMapperFactory(getMappingContext(), getConverter(),
getQueryMappingConfiguration(), getCallbacks(), getPublisher());
}
@Override
@ -121,7 +121,8 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy { @@ -121,7 +121,8 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
JdbcQueryMethod queryMethod = getJdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries);
return new PartTreeJdbcQuery(getMappingContext(), queryMethod, getDialect(), getConverter(), getOperations(), rowMapperFactory);
return new PartTreeJdbcQuery(getMappingContext(), queryMethod, getDialect(), getConverter(), getOperations(),
rowMapperFactory);
}
}
@ -140,10 +141,10 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy { @@ -140,10 +141,10 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
@Nullable BeanFactory beanfactory, ValueExpressionDelegate delegate) {
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
delegate);
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, delegate);
this.rowMapperFactory = new BeanFactoryAwareRowMapperFactory(context, converter, queryMappingConfiguration, callbacks, publisher, beanfactory);
this.rowMapperFactory = new BeanFactoryAwareRowMapperFactory(context, converter, queryMappingConfiguration,
callbacks, publisher, beanfactory);
}
@Override
@ -192,11 +193,10 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy { @@ -192,11 +193,10 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
CreateIfNotFoundQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
CreateQueryLookupStrategy createStrategy,
DeclaredQueryLookupStrategy lookupStrategy, ValueExpressionDelegate delegate) {
CreateQueryLookupStrategy createStrategy, DeclaredQueryLookupStrategy lookupStrategy,
ValueExpressionDelegate delegate) {
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
delegate);
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, delegate);
Assert.notNull(createStrategy, "CreateQueryLookupStrategy must not be null");
Assert.notNull(lookupStrategy, "DeclaredQueryLookupStrategy must not be null");
@ -264,9 +264,9 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy { @@ -264,9 +264,9 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
return switch (keyToUse) {
case CREATE -> createQueryLookupStrategy;
case USE_DECLARED_QUERY -> declaredQueryLookupStrategy;
case CREATE_IF_NOT_FOUND -> new CreateIfNotFoundQueryLookupStrategy(
publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
createQueryLookupStrategy, declaredQueryLookupStrategy, delegate);
case CREATE_IF_NOT_FOUND ->
new CreateIfNotFoundQueryLookupStrategy(publisher, callbacks, context, converter, dialect,
queryMappingConfiguration, operations, createQueryLookupStrategy, declaredQueryLookupStrategy, delegate);
};
}

Loading…
Cancel
Save