diff --git a/pom.xml b/pom.xml
index d91c077f2..e31849e91 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,7 +25,7 @@
5.5.3.Final
8.0.23
42.2.19
- 2.6.0-2228-SNAPSHOT
+ 2.6.0-SNAPSHOT
0.10.3
org.hibernate
diff --git a/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java b/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java
index 798f4663e..c187ccd88 100644
--- a/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java
+++ b/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java
@@ -15,10 +15,10 @@
*/
package org.springframework.data.jpa.repository.support;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
-import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.EntityManager;
@@ -37,6 +37,7 @@ import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.lang.Nullable;
+import org.springframework.util.Assert;
/**
* Immutable implementation of {@link FetchableFluentQuery} based on Query by {@link Example}. All methods that return a
@@ -45,6 +46,7 @@ import org.springframework.lang.Nullable;
* @param Domain type
* @param Result type
* @author Greg Turnquist
+ * @author Mark Paluch
* @since 2.6
*/
class FetchableFluentQueryByExample extends FluentQuerySupport implements FetchableFluentQuery {
@@ -79,24 +81,39 @@ class FetchableFluentQueryByExample extends FluentQuerySupport implemen
this.escapeCharacter = escapeCharacter;
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#sortBy(org.springframework.data.domain.Sort)
+ */
@Override
public FetchableFluentQuery sortBy(Sort sort) {
- return new FetchableFluentQueryByExample(this.example, this.resultType, this.sort.and(sort), this.properties,
+ Assert.notNull(sort, "Sort must not be null!");
+
+ return new FetchableFluentQueryByExample<>(this.example, this.resultType, this.sort.and(sort), this.properties,
this.finder, this.countOperation, this.existsOperation, this.context, this.entityManager, this.escapeCharacter);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#as(java.lang.Class)
+ */
@Override
public FetchableFluentQuery as(Class resultType) {
+ Assert.notNull(resultType, "Projection target type must not be null!");
if (!resultType.isInterface()) {
throw new UnsupportedOperationException("Class-based DTOs are not yet supported.");
}
- return new FetchableFluentQueryByExample(this.example, resultType, this.sort, this.properties, this.finder,
+ return new FetchableFluentQueryByExample<>(this.example, resultType, this.sort, this.properties, this.finder,
this.countOperation, this.existsOperation, this.context, this.entityManager, this.escapeCharacter);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#project(java.util.Collection)
+ */
@Override
public FetchableFluentQuery project(Collection properties) {
@@ -104,62 +121,86 @@ class FetchableFluentQueryByExample extends FluentQuerySupport implemen
this.finder, this.countOperation, this.existsOperation, this.context, this.entityManager, this.escapeCharacter);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#oneValue()
+ */
@Override
public R oneValue() {
TypedQuery limitedQuery = this.finder.apply(this.sort);
limitedQuery.setMaxResults(2); // Never need more than 2 values
- List results = limitedQuery //
- .getResultStream() //
- .map(getConversionFunction(this.example.getProbeType(), this.resultType)) //
- .collect(Collectors.toList());
- ;
+ List results = limitedQuery.getResultList();
if (results.size() > 1) {
throw new IncorrectResultSizeDataAccessException(1);
}
- return results.isEmpty() ? null : results.get(0);
+ return results.isEmpty() ? null : getConversionFunction().apply(results.get(0));
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#firstValue()
+ */
@Override
public R firstValue() {
TypedQuery limitedQuery = this.finder.apply(this.sort);
limitedQuery.setMaxResults(1); // Never need more than 1 value
- List results = limitedQuery //
- .getResultStream() //
- .map(getConversionFunction(this.example.getProbeType(), this.resultType)) //
- .collect(Collectors.toList());
+ List results = limitedQuery.getResultList();
- return results.isEmpty() ? null : results.get(0);
+ return results.isEmpty() ? null : getConversionFunction().apply(results.get(0));
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#all()
+ */
@Override
public List all() {
- return stream().collect(Collectors.toList());
+
+ List resultList = this.finder.apply(this.sort).getResultList();
+
+ return convert(resultList);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#page(org.springframework.data.domain.Pageable)
+ */
@Override
public Page page(Pageable pageable) {
return pageable.isUnpaged() ? new PageImpl<>(all()) : readPage(pageable);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#stream()
+ */
@Override
public Stream stream() {
return this.finder.apply(this.sort) //
.getResultStream() //
- .map(getConversionFunction(this.example.getProbeType(), this.resultType));
+ .map(getConversionFunction());
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#count()
+ */
@Override
public long count() {
return this.countOperation.apply(example);
}
+ /*
+ * (non-Javadoc)
+ * @see org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery#exists()
+ */
@Override
public boolean exists() {
return this.existsOperation.apply(example);
@@ -174,10 +215,24 @@ class FetchableFluentQueryByExample extends FluentQuerySupport implemen
pagedQuery.setMaxResults(pageable.getPageSize());
}
- List paginatedResults = pagedQuery.getResultStream() //
- .map(getConversionFunction(this.example.getProbeType(), this.resultType)) //
- .collect(Collectors.toList());
+ List paginatedResults = convert(pagedQuery.getResultList());
return PageableExecutionUtils.getPage(paginatedResults, pageable, () -> this.countOperation.apply(this.example));
}
+
+ private List convert(List resultList) {
+
+ Function