Browse Source

Polishing.

Refine deprecation messages. Simplify conversion, use Stream methods directly.

See #2138
Original pull request #2161
3.5.x
Mark Paluch 2 months ago
parent
commit
a1e9391897
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 12
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java
  2. 4
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java
  3. 39
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/FetchableFluentQueryByExample.java
  4. 11
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.java
  5. 5
      spring-data-jdbc/src/main/kotlin/org/springframework/data/jdbc/core/JdbcAggregateOperationsExtensions.kt

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

@ -226,9 +226,11 @@ public interface JdbcAggregateOperations {
* @param pageable the pagination information. Must not be {@code null}. * @param pageable the pagination information. Must not be {@code null}.
* @return Guaranteed to be not {@code null}. * @return Guaranteed to be not {@code null}.
* @since 2.0 * @since 2.0
* @deprecated use a combination of other methods of this class to construct results of type {@link Page}. * @deprecated since 3.5.6. Use {@link #findAll(Class, Sort)} together with {@link #count(Class)} to construct results
* of type {@link Page}.The API design is conflicts regarding pagination information. Also, pagination is
* primarily a feature of the repository and not the template API.
*/ */
@Deprecated(since = "4.0") @Deprecated(since = "3.5.6")
<T> Page<T> findAll(Class<T> domainType, Pageable pageable); <T> Page<T> findAll(Class<T> domainType, Pageable pageable);
/** /**
@ -273,9 +275,11 @@ public interface JdbcAggregateOperations {
* @param pageable can be null. * @param pageable can be null.
* @return a {@link Page} of entities matching the given {@link Example}. * @return a {@link Page} of entities matching the given {@link Example}.
* @since 3.0 * @since 3.0
* @deprecated use a combination of other methods of this class to construct results of type {@link Page}. * @deprecated since 3.5.6. Use {@link #findAll(Query, Class)} together with {@link #count(Query, Class)} to construct
* results of type {@link Page}. The API design is conflicts regarding pagination information. Also,
* pagination is primarily a feature of the repository and not the template API.
*/ */
@Deprecated(since = "4.0") @Deprecated(since = "3.5.6")
<T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable); <T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable);
/** /**

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

@ -313,8 +313,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
return allStreamable.map(this::triggerAfterConvert); return allStreamable.map(this::triggerAfterConvert);
} }
@Deprecated
@Override @Override
@Deprecated(since = "3.5.6")
public <T> Page<T> findAll(Class<T> domainType, Pageable pageable) { public <T> Page<T> findAll(Class<T> domainType, Pageable pageable) {
Assert.notNull(domainType, "Domain type must not be null"); Assert.notNull(domainType, "Domain type must not be null");
@ -343,8 +343,8 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
return accessStrategy.streamAll(query, domainType).map(this::triggerAfterConvert); return accessStrategy.streamAll(query, domainType).map(this::triggerAfterConvert);
} }
@Deprecated
@Override @Override
@Deprecated(since = "3.5.6")
public <T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable) { public <T> Page<T> findAll(Query query, Class<T> domainType, Pageable pageable) {
Iterable<T> items = triggerAfterConvert(accessStrategy.findAll(query, domainType, pageable)); Iterable<T> items = triggerAfterConvert(accessStrategy.findAll(query, domainType, pageable));

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

@ -16,13 +16,11 @@
package org.springframework.data.jdbc.repository.support; package org.springframework.data.jdbc.repository.support;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.data.domain.Example; import org.springframework.data.domain.Example;
import org.springframework.data.domain.OffsetScrollPosition; import org.springframework.data.domain.OffsetScrollPosition;
@ -93,16 +91,8 @@ class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> {
private List<R> findAll(Query query) { private List<R> findAll(Query query) {
Function<Object, R> conversionFunction = this.getConversionFunction(); List<S> raw = this.entityOperations.findAll(query, getExampleType());
Iterable<S> raw = this.entityOperations.findAll(query, getExampleType()); return mapContent(raw);
List<R> result = new ArrayList<>(raw instanceof Collections ? ((Collection<?>) raw).size() : 16);
for (S s : raw) {
result.add(conversionFunction.apply(s));
}
return result;
} }
@Override @Override
@ -133,21 +123,33 @@ class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> {
Query contentQuery = createQuery(p -> p.with(pageable)); Query contentQuery = createQuery(p -> p.with(pageable));
List<S> content = this.entityOperations.findAll(contentQuery, getExampleType()); List<S> content = this.entityOperations.findAll(contentQuery, getExampleType());
List<R> result = mapContent(content);
return PageableExecutionUtils.getPage(result, pageable,
() -> this.entityOperations.count(createQuery(), getExampleType()));
}
@SuppressWarnings("unchecked")
private List<R> mapContent(List<S> content) {
Function<Object, R> conversionFunction = getConversionFunction();
if (conversionFunction == Function.identity()) {
return (List<R>) content;
}
List<R> result = new ArrayList<>(content.size()); List<R> result = new ArrayList<>(content.size());
for (S s : content) { for (S s : content) {
result.add(getConversionFunction().apply(s)); result.add(conversionFunction.apply(s));
} }
return PageableExecutionUtils.getPage(result, pageable, () -> this.entityOperations.count(createQuery(), getExampleType())); return result;
} }
@Override @Override
public Stream<R> stream() { public Stream<R> stream() {
return this.entityOperations.streamAll(createQuery().sort(getSort()), getExampleType())
return StreamSupport .map(getConversionFunction());
.stream(this.entityOperations.findAll(createQuery().sort(getSort()), getExampleType()).spliterator(), false)
.map(item -> this.getConversionFunction().apply(item));
} }
@Override @Override
@ -173,7 +175,6 @@ class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> {
} }
query = query.limit(getLimit()); query = query.limit(getLimit());
query = queryCustomizer.apply(query); query = queryCustomizer.apply(query);
return query; return query;

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

@ -147,10 +147,7 @@ public class SimpleJdbcRepository<T, ID>
Assert.notNull(pageable, "Pageable must not be null"); Assert.notNull(pageable, "Pageable must not be null");
Query query1 = Query.query(CriteriaDefinition.empty()); Query query = Query.query(CriteriaDefinition.empty()).with(pageable);
Query query = query1.with(pageable);
List<T> content = entityOperations.findAll(query, entity.getType()); List<T> content = entityOperations.findAll(query, entity.getType());
return PageableExecutionUtils.getPage(content, pageable, () -> entityOperations.count(entity.getType())); return PageableExecutionUtils.getPage(content, pageable, () -> entityOperations.count(entity.getType()));
@ -189,11 +186,7 @@ public class SimpleJdbcRepository<T, ID>
Assert.notNull(pageable, "Pageable must not be null"); Assert.notNull(pageable, "Pageable must not be null");
Query mappedQuery = this.exampleMapper.getMappedExample(example); Query mappedQuery = this.exampleMapper.getMappedExample(example);
List<S> content = this.entityOperations.findAll(mappedQuery.with(pageable), example.getProbeType());
Query contentQuery = mappedQuery.with(pageable);
List<S> content = this.entityOperations.findAll(contentQuery, example.getProbeType());
return PageableExecutionUtils.getPage(content, pageable, return PageableExecutionUtils.getPage(content, pageable,
() -> this.entityOperations.count(mappedQuery, example.getProbeType())); () -> this.entityOperations.count(mappedQuery, example.getProbeType()));

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

@ -20,7 +20,7 @@ import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort import org.springframework.data.domain.Sort
import org.springframework.data.relational.core.query.Query import org.springframework.data.relational.core.query.Query
import java.util.Optional import java.util.*
/** /**
* Kotlin extensions for [JdbcAggregateOperations]. * Kotlin extensions for [JdbcAggregateOperations].
@ -80,7 +80,7 @@ inline fun <reified T> JdbcAggregateOperations.findAll(sort: Sort): List<T> =
/** /**
* Extension for [JdbcAggregateOperations.findAll] with pagination. * Extension for [JdbcAggregateOperations.findAll] with pagination.
*/ */
@Deprecated("Use a combination of operations of this class to construct results of type Page") @Deprecated("Since 3.5.6, use a findAll<T>() and count<T>() to construct results of type Page")
inline fun <reified T> JdbcAggregateOperations.findAll(pageable: Pageable): Page<T> = inline fun <reified T> JdbcAggregateOperations.findAll(pageable: Pageable): Page<T> =
findAll(T::class.java, pageable) findAll(T::class.java, pageable)
@ -99,6 +99,7 @@ inline fun <reified T> JdbcAggregateOperations.findAll(query: Query): List<T> =
/** /**
* Extension for [JdbcAggregateOperations.findAll] with query and pagination. * Extension for [JdbcAggregateOperations.findAll] with query and pagination.
*/ */
@Deprecated("Since 3.5.6, use a findAll<T>(Query) and count<T>(Query) to construct results of type Page")
inline fun <reified T> JdbcAggregateOperations.findAll(query: Query, pageable: Pageable): Page<T> = inline fun <reified T> JdbcAggregateOperations.findAll(query: Query, pageable: Pageable): Page<T> =
findAll(query, T::class.java, pageable) findAll(query, T::class.java, pageable)

Loading…
Cancel
Save