Browse Source

Deprecate methods accepting `Query` and `Pageable` returning `Page`.

Since both the Query and the Pageable have limit, offset and sorting information, these methods are confusing and the strategy to resolve the conflict is not clear.

Existing code using the methods got replaced.

Users of these methods should use the various findAll and findBy methods to construct Page objects as desired.

Closes #2138
Original pull request #2161
pull/2163/head
Jens Schauder 2 months ago committed by Mark Paluch
parent
commit
dc8195f9c4
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 4
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java
  2. 2
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java
  3. 12
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/FetchableFluentQueryByExample.java
  4. 27
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java
  5. 1
      spring-data-jdbc/src/main/kotlin/org/springframework/data/jdbc/core/JdbcAggregateOperationsExtensions.kt
  6. 4
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java
  7. 3
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/SimpleJdbcRepositoryEventsUnitTests.java

4
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java

@ -230,7 +230,9 @@ public interface JdbcAggregateOperations { @@ -230,7 +230,9 @@ public interface JdbcAggregateOperations {
* @param pageable the pagination information. Must not be {@code null}.
* @return Guaranteed to be not {@code null}.
* @since 2.0
* @deprecated use a combination of other methods of this class to construct results of type {@link Page}.
*/
@Deprecated(since = "4.0")
<T> Page<T> findAll(Class<T> domainType, Pageable pageable);
/**
@ -275,7 +277,9 @@ public interface JdbcAggregateOperations { @@ -275,7 +277,9 @@ public interface JdbcAggregateOperations {
* @param pageable can be null.
* @return a {@link Page} of entities matching the given {@link Example}.
* @since 3.0
* @deprecated use a combination of other methods of this class to construct results of type {@link Page}.
*/
@Deprecated(since = "4.0")
<T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable);
/**

2
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

@ -359,6 +359,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations, Applicati @@ -359,6 +359,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations, Applicati
return allStreamable.map(this::triggerAfterConvert);
}
@Deprecated
@Override
public <T> Page<T> findAll(Class<T> domainType, Pageable pageable) {
@ -388,6 +389,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations, Applicati @@ -388,6 +389,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations, Applicati
return accessStrategy.streamAll(query, domainType).map(this::triggerAfterConvert);
}
@Deprecated
@Override
public <T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable) {

12
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/FetchableFluentQueryByExample.java

@ -35,6 +35,7 @@ import org.springframework.data.jdbc.core.JdbcAggregateOperations; @@ -35,6 +35,7 @@ import org.springframework.data.jdbc.core.JdbcAggregateOperations;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.repository.query.RelationalExampleMapper;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.util.Assert;
/**
@ -126,8 +127,15 @@ class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> { @@ -126,8 +127,15 @@ class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> {
@Override
public Page<R> page(Pageable pageable) {
return this.entityOperations.findAll(createQuery(p -> p.with(pageable)), getExampleType(), pageable)
.map(item -> this.getConversionFunction().apply(item));
Query contentQuery = createQuery(p -> p.with(pageable));
List<S> content = this.entityOperations.findAll(contentQuery, getExampleType());
List<R> result = new ArrayList<>(content.size());
for (S s : content) {
result.add(getConversionFunction().apply(s));
}
return PageableExecutionUtils.getPage(result, pageable, () -> this.entityOperations.count(createQuery(), getExampleType()));
}
@Override

27
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java

@ -27,11 +27,14 @@ import org.springframework.data.jdbc.core.JdbcAggregateOperations; @@ -27,11 +27,14 @@ import org.springframework.data.jdbc.core.JdbcAggregateOperations;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.relational.core.query.CriteriaDefinition;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.relational.repository.query.RelationalExampleMapper;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.FluentQuery;
import org.springframework.data.repository.query.QueryByExampleExecutor;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
@ -139,7 +142,16 @@ public class SimpleJdbcRepository<T, ID> @@ -139,7 +142,16 @@ public class SimpleJdbcRepository<T, ID>
@Override
public Page<T> findAll(Pageable pageable) {
return entityOperations.findAll(entity.getType(), pageable);
Assert.notNull(pageable, "Pageable must not be null");
Query query1 = Query.query(CriteriaDefinition.empty());
Query query = query1.with(pageable);
List<T> content = entityOperations.findAll(query, entity.getType());
return PageableExecutionUtils.getPage(content, pageable, () -> entityOperations.count(entity.getType()));
}
@Override
@ -172,9 +184,17 @@ public class SimpleJdbcRepository<T, ID> @@ -172,9 +184,17 @@ public class SimpleJdbcRepository<T, ID>
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
Assert.notNull(example, "Example must not be null");
Assert.notNull(pageable, "Pageable must not be null");
return this.entityOperations.findAll(this.exampleMapper.getMappedExample(example), example.getProbeType(),
pageable);
Query mappedQuery = this.exampleMapper.getMappedExample(example);
Query contentQuery = mappedQuery.with(pageable);
List<S> content = this.entityOperations.findAll(contentQuery, example.getProbeType());
return PageableExecutionUtils.getPage(content, pageable,
() -> this.entityOperations.count(mappedQuery, example.getProbeType()));
}
@Override
@ -203,4 +223,5 @@ public class SimpleJdbcRepository<T, ID> @@ -203,4 +223,5 @@ public class SimpleJdbcRepository<T, ID>
return queryFunction.apply(fluentQuery);
}
}

1
spring-data-jdbc/src/main/kotlin/org/springframework/data/jdbc/core/JdbcAggregateOperationsExtensions.kt

@ -80,6 +80,7 @@ inline fun <reified T : Any> JdbcAggregateOperations.findAll(sort: Sort): List<T @@ -80,6 +80,7 @@ inline fun <reified T : Any> JdbcAggregateOperations.findAll(sort: Sort): List<T
/**
* Extension for [JdbcAggregateOperations.findAll] with pagination.
*/
@Deprecated("Use a combination of operations of this class to construct results of type Page")
inline fun <reified T : Any> JdbcAggregateOperations.findAll(pageable: Pageable): Page<T> =
findAll(T::class.java, pageable)

4
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

@ -119,6 +119,7 @@ public class JdbcRepositoryIntegrationTests { @@ -119,6 +119,7 @@ public class JdbcRepositoryIntegrationTests {
public static Stream<Arguments> findAllByExamplePageableSource() {
// Pageable pageRequest, int size, int totalPages, List<String> notContains
return Stream.of( //
Arguments.of(PageRequest.of(0, 3), 3, 34, Arrays.asList("3", "4", "100")), //
Arguments.of(PageRequest.of(1, 10), 10, 10, Arrays.asList("9", "20", "30")), //
@ -126,7 +127,8 @@ public class JdbcRepositoryIntegrationTests { @@ -126,7 +127,8 @@ public class JdbcRepositoryIntegrationTests {
Arguments.of(PageRequest.of(33, 3), 1, 34, Collections.emptyList()), //
Arguments.of(PageRequest.of(36, 3), 0, 34, Collections.emptyList()), //
Arguments.of(PageRequest.of(0, 10000), 100, 1, Collections.emptyList()), //
Arguments.of(PageRequest.of(100, 10000), 0, 1, Collections.emptyList()) //
Arguments.of(PageRequest.of(100, 10000), 0, 1, Collections.emptyList()), //
Arguments.of(Pageable.unpaged(), 100, 1, Collections.emptyList()) //
);
}

3
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/SimpleJdbcRepositoryEventsUnitTests.java

@ -51,6 +51,7 @@ import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent; @@ -51,6 +51,7 @@ import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent;
import org.springframework.data.relational.core.mapping.event.Identifier;
import org.springframework.data.relational.core.mapping.event.RelationalEvent;
import org.springframework.data.relational.core.mapping.event.WithId;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.jdbc.core.JdbcOperations;
@ -264,7 +265,7 @@ class SimpleJdbcRepositoryEventsUnitTests { @@ -264,7 +265,7 @@ class SimpleJdbcRepositoryEventsUnitTests {
DummyEntity entity1 = new DummyEntity(42L);
DummyEntity entity2 = new DummyEntity(23L);
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAll(any(), any(Pageable.class));
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAll(any(Query.class), any(Class.class));
doReturn(2L).when(dataAccessStrategy).count(any());
repository.findAll(PageRequest.of(0, 20));

Loading…
Cancel
Save