Skip to content

Commit bc0a58b

Browse files
DATAMONGO-1245 - Align MongoRepsoitory QBE with DATAJPA-218.
1 parent 69e81a3 commit bc0a58b

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2014 the original author or authors.
2+
* Copyright 2010-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,17 +76,32 @@ public interface MongoRepository<T, ID extends Serializable> extends PagingAndSo
7676
<S extends T> List<S> insert(Iterable<S> entities);
7777

7878
/**
79-
* @param example
79+
* Returns all instances of the type specified by the given {@link Example}.
80+
*
81+
* @param example must not be {@literal null}.
8082
* @return
8183
* @since 1.8
8284
*/
8385
<S extends T> List<T> findAllByExample(Example<S> example);
8486

8587
/**
86-
* @param example
87-
* @param pageable
88-
* @return
88+
* Returns all instances of the type specified by the given {@link Example}.
89+
*
90+
* @param example must not be {@literal null}.
91+
* @param sort can be {@literal null}.
92+
* @return all entities sorted by the given options
93+
* @since 1.8
94+
*/
95+
<S extends T> List<T> findAllByExample(Example<S> example, Sort sort);
96+
97+
/**
98+
* Returns a {@link Page} of entities meeting the paging restriction specified by the given {@link Example} limited to
99+
* criteria provided in the {@code Pageable} object.
100+
*
101+
* @param example must not be {@literal null}.
102+
* @param pageable can be {@literal null}.
103+
* @return a {@link Page} of entities
89104
* @since 1.8
90105
*/
91-
<S extends T> Page<T> findByExample(Example<S> example, Pageable pageable);
106+
<S extends T> Page<T> findAllByExample(Example<S> example, Pageable pageable);
92107
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ public <S extends T> List<S> insert(Iterable<S> entities) {
262262

263263
/*
264264
* (non-Javadoc)
265-
* @see org.springframework.data.mongodb.repository.MongoRepository#findByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Pageable)
265+
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Pageable)
266266
*/
267267
@Override
268-
public <S extends T> Page<T> findByExample(Example<S> example, Pageable pageable) {
268+
public <S extends T> Page<T> findAllByExample(Example<S> example, Pageable pageable) {
269269

270270
Assert.notNull(example, "Sample must not be null!");
271271

@@ -279,6 +279,24 @@ public <S extends T> Page<T> findByExample(Example<S> example, Pageable pageable
279279
entityInformation.getCollectionName()), pageable, count);
280280
}
281281

282+
/*
283+
* (non-Javadoc)
284+
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
285+
*/
286+
@Override
287+
public <S extends T> List<T> findAllByExample(Example<S> example, Sort sort) {
288+
289+
Assert.notNull(example, "Sample must not be null!");
290+
291+
Query q = new Query(new Criteria().alike(example));
292+
293+
if (sort != null) {
294+
q.with(sort);
295+
}
296+
297+
return findAll(q);
298+
}
299+
282300
/*
283301
* (non-Javadoc)
284302
* @see org.springframework.data.mongodb.repository.MongoRepository#findAllByExample(org.springframework.data.domain.Example)

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ public void findByExampleShouldResolveStuffCorrectly() {
12401240
ReflectionTestUtils.setField(sample, "createdAt", null);
12411241
ReflectionTestUtils.setField(sample, "email", null);
12421242

1243-
Page<Person> result = repository.findByExample(new Example<Person>(sample), new PageRequest(0, 10));
1243+
Page<Person> result = repository.findAllByExample(new Example<Person>(sample), new PageRequest(0, 10));
12441244
Assert.assertThat(result.getNumberOfElements(), Is.is(2));
12451245
}
12461246

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public void findByExampleShouldLookUpEntriesCorrectly() {
180180
sample.setLastname("Matthews");
181181
trimDomainType(sample, "id", "createdAt", "email");
182182

183-
Page<Person> result = repository.findByExample(new Example<Person>(sample), new PageRequest(0, 10));
183+
Page<Person> result = repository.findAllByExample(new Example<Person>(sample), new PageRequest(0, 10));
184184

185185
assertThat(result.getContent(), hasItems(dave, oliver));
186186
assertThat(result.getContent(), hasSize(2));

0 commit comments

Comments
 (0)