Skip to content

Commit 3349454

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1075 - Containing keyword is now correctly translated for collection properties.
We now inspect the properties type when creating criteria for CONTAINS keyword so that, if the target property is of type String, we use an expression, and if the property is collection like we try to finds an exact match within the collection using $in. Original pull request: #241.
1 parent c04343a commit 3349454

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2013 the original author or authors.
2+
* Copyright 2010-2014 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.
@@ -46,6 +46,7 @@
4646
*
4747
* @author Oliver Gierke
4848
* @author Thomas Darimont
49+
* @author Christoph Strobl
4950
*/
5051
class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
5152

@@ -198,7 +199,7 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
198199
case STARTING_WITH:
199200
case ENDING_WITH:
200201
case CONTAINING:
201-
return addAppropriateLikeRegexTo(criteria, part, parameters.next().toString());
202+
return createContainingCriteria(part, property, criteria, parameters);
202203
case REGEX:
203204
return criteria.regex(parameters.next().toString());
204205
case EXISTS:
@@ -292,6 +293,27 @@ private Criteria createLikeRegexCriteriaOrThrow(Part part, MongoPersistentProper
292293
Arrays.asList(IgnoreCaseType.ALWAYS, IgnoreCaseType.WHEN_POSSIBLE), part.shouldIgnoreCase()));
293294
}
294295

296+
/**
297+
* If the target property of the comparison is of type String, then the operator checks for match using regular
298+
* expression. If the target property of the comparison is a {@link Collection} then the operator evaluates to true if
299+
* it finds an exact match within any member of the {@link Collection}.
300+
*
301+
* @param part
302+
* @param property
303+
* @param criteria
304+
* @param parameters
305+
* @return
306+
*/
307+
private Criteria createContainingCriteria(Part part, MongoPersistentProperty property, Criteria criteria,
308+
PotentiallyConvertingIterator parameters) {
309+
310+
if (property.isCollectionLike()) {
311+
return criteria.in(nextAsArray(parameters, property));
312+
}
313+
314+
return addAppropriateLikeRegexTo(criteria, part, parameters.next().toString());
315+
}
316+
295317
/**
296318
* Creates an appropriate like-regex and appends it to the given criteria.
297319
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,20 @@ public void shouldCreateDeleteByQueryCorrectlyForMultipleCriteriaAndCaseExpressi
438438
assertThat(query, is(query(where("firstName").regex("^dave$", "i").and("age").is(42))));
439439
}
440440

441+
/**
442+
* @see DATAMONGO-1075
443+
*/
444+
@Test
445+
public void shouldCreateInClauseWhenUsingContainsOnCollectionLikeProperty() {
446+
447+
PartTree tree = new PartTree("findByEmailAddressesContaining", User.class);
448+
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "dave"), context);
449+
450+
Query query = creator.createQuery();
451+
452+
assertThat(query, is(query(where("emailAddresses").in("dave"))));
453+
}
454+
441455
interface PersonRepository extends Repository<Person, Long> {
442456

443457
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);
@@ -448,5 +462,7 @@ class User {
448462
@Field("foo") String username;
449463

450464
@DBRef User creator;
465+
466+
List<String> emailAddresses;
451467
}
452468
}

0 commit comments

Comments
 (0)