Skip to content

Commit 9cad171

Browse files
committed
Let QueryByExampleRedisExecutor implement ListQueryByExampleExecutor.
Closes #2880
1 parent 4c8941d commit 9cad171

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

src/main/antora/modules/ROOT/pages/redis/redis-repositories/query-by-example.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The following example uses Query by Example against a repository:
99
====
1010
[source, java]
1111
----
12-
interface PersonRepository extends QueryByExampleExecutor<Person> {
12+
interface PersonRepository extends ListQueryByExampleExecutor<Person> {
1313
}
1414
1515
class PersonService {

src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private RedisQueryEngine(CriteriaAccessor<RedisOperationChain> criteriaAccessor,
8181

8282
@Override
8383
@SuppressWarnings("unchecked")
84-
public <T> Collection<T> execute(RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
84+
public <T> List<T> execute(RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
8585
String keyspace, Class<T> type) {
8686
List<T> result = doFind(criteria, offset, rows, keyspace, type);
8787

@@ -199,7 +199,7 @@ private List<byte[]> findKeys(RedisOperationChain criteria, int rows, String key
199199
}
200200

201201
@Override
202-
public Collection<?> execute(RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
202+
public List<?> execute(RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
203203
String keyspace) {
204204
return execute(criteria, sort, offset, rows, keyspace, Object.class);
205205
}

src/main/java/org/springframework/data/redis/repository/support/QueryByExampleRedisExecutor.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.data.redis.repository.query.RedisOperationChain;
4545
import org.springframework.data.repository.core.EntityInformation;
4646
import org.springframework.data.repository.query.FluentQuery;
47+
import org.springframework.data.repository.query.ListQueryByExampleExecutor;
4748
import org.springframework.data.repository.query.QueryByExampleExecutor;
4849
import org.springframework.data.support.PageableExecutionUtils;
4950
import org.springframework.data.util.Streamable;
@@ -62,7 +63,7 @@
6263
*/
6364
@SuppressWarnings("unchecked")
6465
public class QueryByExampleRedisExecutor<T>
65-
implements QueryByExampleExecutor<T>, BeanFactoryAware, BeanClassLoaderAware {
66+
implements ListQueryByExampleExecutor<T>, BeanFactoryAware, BeanClassLoaderAware {
6667

6768
private final EntityInformation<T, ?> entityInformation;
6869
private final RedisKeyValueTemplate keyValueTemplate;
@@ -147,15 +148,17 @@ private <S extends T> Iterator<S> doFind(Example<S> example) {
147148
}
148149

149150
@Override
150-
public <S extends T> Iterable<S> findAll(Example<S> example) {
151+
public <S extends T> List<S> findAll(Example<S> example) {
151152

152153
RedisOperationChain operationChain = createQuery(example);
153154

154-
return (Iterable<S>) keyValueTemplate.find(new KeyValueQuery<>(operationChain), entityInformation.getJavaType());
155+
Iterable<T> result = keyValueTemplate.find(new KeyValueQuery<>(operationChain), entityInformation.getJavaType());
156+
157+
return (List<S>) (result instanceof List<?> list ? list : Streamable.of(result).toList());
155158
}
156159

157160
@Override
158-
public <S extends T> Iterable<S> findAll(Example<S> example, Sort sort) {
161+
public <S extends T> List<S> findAll(Example<S> example, Sort sort) {
159162
throw new UnsupportedOperationException("Ordering is not supported");
160163
}
161164

src/test/java/org/springframework/data/redis/repository/support/QueryByExampleRedisExecutorIntegrationTests.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package org.springframework.data.redis.repository.support;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
18+
import static org.assertj.core.api.Assertions.*;
2119

2220
import java.util.Arrays;
2321
import java.util.List;
@@ -57,7 +55,7 @@
5755
* @author Christoph Strobl
5856
* @author John Blum
5957
*/
60-
public class QueryByExampleRedisExecutorIntegrationTests {
58+
class QueryByExampleRedisExecutorIntegrationTests {
6159

6260
private static JedisConnectionFactory connectionFactory;
6361
private RedisMappingContext mappingContext = new RedisMappingContext();
@@ -129,7 +127,7 @@ void shouldNotFindOneByExample() {
129127
assertThat(result).isEmpty();
130128
}
131129

132-
@Test // DATAREDIS-605
130+
@Test // DATAREDIS-605, GH-2880
133131
void shouldFindAllByExample() {
134132

135133
QueryByExampleRedisExecutor<Person> executor = new QueryByExampleRedisExecutor<>(getEntityInformation(Person.class),
@@ -138,7 +136,7 @@ void shouldFindAllByExample() {
138136
Person person = new Person();
139137
person.setHometown(walt.getHometown());
140138

141-
Iterable<Person> result = executor.findAll(Example.of(person));
139+
List<Person> result = executor.findAll(Example.of(person));
142140
assertThat(result).contains(walt, gus, hank);
143141
}
144142

0 commit comments

Comments
 (0)