Skip to content

Commit e644692

Browse files
christophstroblmp911de
authored andcommitted
Fix Criteria chaining for Criteria.alike().
This commit fixes an issue where an Example probe would not be added to the criteria chain. Closes #3544 Original pull request: #3549.
1 parent 1d547ec commit e644692

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,15 @@ public Criteria elemMatch(Criteria criteria) {
620620
*/
621621
public Criteria alike(Example<?> sample) {
622622

623-
criteria.put("$example", sample);
624-
return this;
623+
if (StringUtils.hasText(this.getKey())) {
624+
625+
criteria.put("$example", sample);
626+
return this;
627+
}
628+
629+
Criteria exampleCriteria = new Criteria();
630+
exampleCriteria.criteria.put("$example", sample);
631+
return registerCriteriaChainElement(exampleCriteria);
625632
}
626633

627634
/**

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,14 @@ public void findByExampleShouldReturnEverythingWhenSampleIsEmpty() {
139139
assertThat(result).containsExactlyInAnyOrder(p1, p2, p3);
140140
}
141141

142-
@Test // DATAMONGO-1245
142+
@Test // DATAMONGO-1245, GH-3544
143143
public void findByExampleWithCriteria() {
144144

145145
Person sample = new Person();
146146
sample.lastname = "stark";
147147

148-
Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex("^ary*"));
149-
150-
List<Person> result = operations.find(query, Person.class);
151-
assertThat(result).hasSize(1);
148+
Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex(".*n.*"));
149+
assertThat(operations.find(query, Person.class)).containsExactly(p1);
152150
}
153151

154152
@Test // DATAMONGO-1459

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,19 @@ void exampleShouldBeMappedCorrectlyWhenContainingLegacyPoint() {
771771
assertThat(document).containsEntry("legacyPoint.y", 20D);
772772
}
773773

774+
@Test // GH-3544
775+
void exampleWithCombinedCriteriaShouldBeMappedCorrectly() {
776+
777+
Foo probe = new Foo();
778+
probe.embedded = new EmbeddedClass();
779+
probe.embedded.id = "conflux";
780+
781+
Query query = query(byExample(probe).and("listOfItems").exists(true));
782+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
783+
784+
assertThat(document).containsEntry("embedded\\._id", "conflux").containsEntry("my_items", new org.bson.Document("$exists", true));
785+
}
786+
774787
@Test // DATAMONGO-1988
775788
void mapsStringObjectIdRepresentationToObjectIdWhenReferencingIdProperty() {
776789

0 commit comments

Comments
 (0)