Skip to content

Commit dc8195f

Browse files
schaudermp911de
authored andcommitted
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
1 parent 3273572 commit dc8195f

File tree

7 files changed

+46
-7
lines changed

7 files changed

+46
-7
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ public interface JdbcAggregateOperations {
230230
* @param pageable the pagination information. Must not be {@code null}.
231231
* @return Guaranteed to be not {@code null}.
232232
* @since 2.0
233+
* @deprecated use a combination of other methods of this class to construct results of type {@link Page}.
233234
*/
235+
@Deprecated(since = "4.0")
234236
<T> Page<T> findAll(Class<T> domainType, Pageable pageable);
235237

236238
/**
@@ -275,7 +277,9 @@ public interface JdbcAggregateOperations {
275277
* @param pageable can be null.
276278
* @return a {@link Page} of entities matching the given {@link Example}.
277279
* @since 3.0
280+
* @deprecated use a combination of other methods of this class to construct results of type {@link Page}.
278281
*/
282+
@Deprecated(since = "4.0")
279283
<T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable);
280284

281285
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ public <T> Stream<T> streamAll(Class<T> domainType, Sort sort) {
359359
return allStreamable.map(this::triggerAfterConvert);
360360
}
361361

362+
@Deprecated
362363
@Override
363364
public <T> Page<T> findAll(Class<T> domainType, Pageable pageable) {
364365

@@ -388,6 +389,7 @@ public <T> Stream<T> streamAll(Query query, Class<T> domainType) {
388389
return accessStrategy.streamAll(query, domainType).map(this::triggerAfterConvert);
389390
}
390391

392+
@Deprecated
391393
@Override
392394
public <T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable) {
393395

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.projection.ProjectionFactory;
3636
import org.springframework.data.relational.core.query.Query;
3737
import org.springframework.data.relational.repository.query.RelationalExampleMapper;
38+
import org.springframework.data.support.PageableExecutionUtils;
3839
import org.springframework.util.Assert;
3940

4041
/**
@@ -126,8 +127,15 @@ public Window<R> scroll(ScrollPosition scrollPosition) {
126127
@Override
127128
public Page<R> page(Pageable pageable) {
128129

129-
return this.entityOperations.findAll(createQuery(p -> p.with(pageable)), getExampleType(), pageable)
130-
.map(item -> this.getConversionFunction().apply(item));
130+
Query contentQuery = createQuery(p -> p.with(pageable));
131+
List<S> content = this.entityOperations.findAll(contentQuery, getExampleType());
132+
133+
List<R> result = new ArrayList<>(content.size());
134+
for (S s : content) {
135+
result.add(getConversionFunction().apply(s));
136+
}
137+
138+
return PageableExecutionUtils.getPage(result, pageable, () -> this.entityOperations.count(createQuery(), getExampleType()));
131139
}
132140

133141
@Override

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2828
import org.springframework.data.mapping.PersistentEntity;
2929
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
30+
import org.springframework.data.relational.core.query.CriteriaDefinition;
31+
import org.springframework.data.relational.core.query.Query;
3032
import org.springframework.data.relational.repository.query.RelationalExampleMapper;
3133
import org.springframework.data.repository.CrudRepository;
3234
import org.springframework.data.repository.PagingAndSortingRepository;
3335
import org.springframework.data.repository.query.FluentQuery;
3436
import org.springframework.data.repository.query.QueryByExampleExecutor;
37+
import org.springframework.data.support.PageableExecutionUtils;
3538
import org.springframework.transaction.annotation.Transactional;
3639
import org.springframework.util.Assert;
3740

@@ -139,7 +142,16 @@ public List<T> findAll(Sort sort) {
139142

140143
@Override
141144
public Page<T> findAll(Pageable pageable) {
142-
return entityOperations.findAll(entity.getType(), pageable);
145+
146+
Assert.notNull(pageable, "Pageable must not be null");
147+
148+
Query query1 = Query.query(CriteriaDefinition.empty());
149+
150+
151+
Query query = query1.with(pageable);
152+
List<T> content = entityOperations.findAll(query, entity.getType());
153+
154+
return PageableExecutionUtils.getPage(content, pageable, () -> entityOperations.count(entity.getType()));
143155
}
144156

145157
@Override
@@ -172,9 +184,17 @@ public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
172184
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
173185

174186
Assert.notNull(example, "Example must not be null");
187+
Assert.notNull(pageable, "Pageable must not be null");
175188

176-
return this.entityOperations.findAll(this.exampleMapper.getMappedExample(example), example.getProbeType(),
177-
pageable);
189+
Query mappedQuery = this.exampleMapper.getMappedExample(example);
190+
191+
192+
Query contentQuery = mappedQuery.with(pageable);
193+
194+
List<S> content = this.entityOperations.findAll(contentQuery, example.getProbeType());
195+
196+
return PageableExecutionUtils.getPage(content, pageable,
197+
() -> this.entityOperations.count(mappedQuery, example.getProbeType()));
178198
}
179199

180200
@Override
@@ -203,4 +223,5 @@ public <S extends T, R> R findBy(Example<S> example, Function<FluentQuery.Fetcha
203223

204224
return queryFunction.apply(fluentQuery);
205225
}
226+
206227
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ inline fun <reified T : Any> JdbcAggregateOperations.findAll(sort: Sort): List<T
8080
/**
8181
* Extension for [JdbcAggregateOperations.findAll] with pagination.
8282
*/
83+
@Deprecated("Use a combination of operations of this class to construct results of type Page")
8384
inline fun <reified T : Any> JdbcAggregateOperations.findAll(pageable: Pageable): Page<T> =
8485
findAll(T::class.java, pageable)
8586

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ public class JdbcRepositoryIntegrationTests {
119119

120120
public static Stream<Arguments> findAllByExamplePageableSource() {
121121

122+
// Pageable pageRequest, int size, int totalPages, List<String> notContains
122123
return Stream.of( //
123124
Arguments.of(PageRequest.of(0, 3), 3, 34, Arrays.asList("3", "4", "100")), //
124125
Arguments.of(PageRequest.of(1, 10), 10, 10, Arrays.asList("9", "20", "30")), //
125126
Arguments.of(PageRequest.of(2, 10), 10, 10, Arrays.asList("1", "2", "3")), //
126127
Arguments.of(PageRequest.of(33, 3), 1, 34, Collections.emptyList()), //
127128
Arguments.of(PageRequest.of(36, 3), 0, 34, Collections.emptyList()), //
128129
Arguments.of(PageRequest.of(0, 10000), 100, 1, Collections.emptyList()), //
129-
Arguments.of(PageRequest.of(100, 10000), 0, 1, Collections.emptyList()) //
130+
Arguments.of(PageRequest.of(100, 10000), 0, 1, Collections.emptyList()), //
131+
Arguments.of(Pageable.unpaged(), 100, 1, Collections.emptyList()) //
130132
);
131133
}
132134

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.data.relational.core.mapping.event.Identifier;
5252
import org.springframework.data.relational.core.mapping.event.RelationalEvent;
5353
import org.springframework.data.relational.core.mapping.event.WithId;
54+
import org.springframework.data.relational.core.query.Query;
5455
import org.springframework.data.repository.CrudRepository;
5556
import org.springframework.data.repository.PagingAndSortingRepository;
5657
import org.springframework.jdbc.core.JdbcOperations;
@@ -264,7 +265,7 @@ void publishesEventsOnFindAllPaged() {
264265
DummyEntity entity1 = new DummyEntity(42L);
265266
DummyEntity entity2 = new DummyEntity(23L);
266267

267-
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAll(any(), any(Pageable.class));
268+
doReturn(asList(entity1, entity2)).when(dataAccessStrategy).findAll(any(Query.class), any(Class.class));
268269
doReturn(2L).when(dataAccessStrategy).count(any());
269270

270271
repository.findAll(PageRequest.of(0, 20));

0 commit comments

Comments
 (0)