Skip to content

Commit 0aff94b

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1425 - Fix query derivation for notContaining on String properties.
We now correctly build up the criteria for derived queries using notContaining keyword on String properties. Original pull request: #363.
1 parent 87657aa commit 0aff94b

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
201201
case CONTAINING:
202202
return createContainingCriteria(part, property, criteria, parameters);
203203
case NOT_CONTAINING:
204-
return createContainingCriteria(part, property, criteria, parameters).not();
204+
return createContainingCriteria(part, property, criteria.not(), parameters);
205205
case REGEX:
206206
return criteria.regex(parameters.next().toString());
207207
case EXISTS:

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,14 +1211,14 @@ public void shouldFindByFirstnameAndCurrentUserWithCustomQuery() {
12111211
}
12121212

12131213
/**
1214-
* @see DATAMONGO-990
1214+
* @see DATAMONGO-1425
12151215
*/
12161216
@Test
1217-
public void shouldFindByFirstnameForSpELExpressionWithParameterVariableOnly() {
1218-
1219-
List<Person> users = repository.findWithSpelByFirstnameForSpELExpressionWithParameterVariableOnly("Dave");
1217+
public void findsPersonsByFirstnameNotContains() throws Exception {
12201218

1221-
assertThat(users, hasSize(1));
1222-
assertThat(users.get(0), is(dave));
1219+
List<Person> result = repository.findByFirstnameNotContains("Boyd");
1220+
assertThat(result.size(), is((int) (repository.count() - 1)));
1221+
assertThat(result, not(hasItem(boyd)));
12231222
}
1223+
12241224
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
8989
*/
9090
List<Person> findByFirstnameLike(String firstname);
9191

92+
List<Person> findByFirstnameNotContains(String firstname);
93+
9294
List<Person> findByFirstnameLikeOrderByLastnameAsc(String firstname, Sort sort);
9395

9496
@Query("{'age' : { '$lt' : ?0 } }")
@@ -309,7 +311,8 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
309311
* @see DATAMONGO-745
310312
*/
311313
@Query("{lastname:?0, address.street:{$in:?1}}")
312-
Page<Person> findByCustomQueryLastnameAndAddressStreetInList(String lastname, List<String> streetNames, Pageable page);
314+
Page<Person> findByCustomQueryLastnameAndAddressStreetInList(String lastname, List<String> streetNames,
315+
Pageable page);
313316

314317
/**
315318
* @see DATAMONGO-950
@@ -334,19 +337,19 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
334337
*/
335338
@Query("{ firstname : { $in : ?0 }}")
336339
Stream<Person> findByCustomQueryWithStreamingCursorByFirstnames(List<String> firstnames);
337-
340+
338341
/**
339342
* @see DATAMONGO-990
340343
*/
341344
@Query("{ firstname : ?#{[0]}}")
342345
List<Person> findWithSpelByFirstnameForSpELExpressionWithParameterIndexOnly(String firstname);
343-
346+
344347
/**
345348
* @see DATAMONGO-990
346349
*/
347350
@Query("{ firstname : ?#{[0]}, email: ?#{principal.email} }")
348351
List<Person> findWithSpelByFirstnameAndCurrentUserWithCustomQuery(String firstname);
349-
352+
350353
/**
351354
* @see DATAMONGO-990
352355
*/

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ public void shouldCreateInClauseWhenUsingNotContainsOnCollectionLikeProperty() {
461461

462462
/**
463463
* @see DATAMONGO-1075
464+
* @see DATAMONGO-1425
464465
*/
465466
@Test
466467
public void shouldCreateRegexWhenUsingNotContainsOnStringProperty() {
@@ -469,7 +470,7 @@ public void shouldCreateRegexWhenUsingNotContainsOnStringProperty() {
469470
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "thew"), context);
470471
Query query = creator.createQuery();
471472

472-
assertThat(query, is(query(where("username").regex(".*thew.*").not())));
473+
assertThat(query.getQueryObject(), is(query(where("username").not().regex(".*thew.*")).getQueryObject()));
473474
}
474475

475476
/**

0 commit comments

Comments
 (0)