Skip to content

Commit 0cf6eda

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 0824105 commit 0cf6eda

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
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
@@ -200,7 +200,7 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
200200
case CONTAINING:
201201
return createContainingCriteria(part, property, criteria, parameters);
202202
case NOT_CONTAINING:
203-
return createContainingCriteria(part, property, criteria, parameters).not();
203+
return createContainingCriteria(part, property, criteria.not(), parameters);
204204
case REGEX:
205205
return criteria.regex(parameters.next().toString());
206206
case EXISTS:

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,4 +1261,15 @@ public void findAllByExampleShouldResolveStuffCorrectly() {
12611261
assertThat(result.size(), is(2));
12621262
}
12631263

1264+
/**
1265+
* @see DATAMONGO-1425
1266+
*/
1267+
@Test
1268+
public void findsPersonsByFirstnameNotContains() throws Exception {
1269+
1270+
List<Person> result = repository.findByFirstnameNotContains("Boyd");
1271+
assertThat(result.size(), is((int) (repository.count() - 1)));
1272+
assertThat(result, not(hasItem(boyd)));
1273+
}
1274+
12641275
}

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
@@ -454,6 +454,7 @@ public void shouldCreateInClauseWhenUsingNotContainsOnCollectionLikeProperty() {
454454

455455
/**
456456
* @see DATAMONGO-1075
457+
* @see DATAMONGO-1425
457458
*/
458459
@Test
459460
public void shouldCreateRegexWhenUsingNotContainsOnStringProperty() {
@@ -462,7 +463,7 @@ public void shouldCreateRegexWhenUsingNotContainsOnStringProperty() {
462463
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "thew"), context);
463464
Query query = creator.createQuery();
464465

465-
assertThat(query, is(query(where("username").regex(".*thew.*").not())));
466+
assertThat(query.getQueryObject(), is(query(where("username").not().regex(".*thew.*")).getQueryObject()));
466467
}
467468

468469
/**

0 commit comments

Comments
 (0)