Browse Source

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.
2.2.x
Christoph Strobl 5 years ago committed by Mark Paluch
parent
commit
fc3930663a
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java
  2. 8
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/QueryByExampleTests.java
  3. 13
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

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

@ -615,10 +615,17 @@ public class Criteria implements CriteriaDefinition { @@ -615,10 +615,17 @@ public class Criteria implements CriteriaDefinition {
*/
public Criteria alike(Example<?> sample) {
if (StringUtils.hasText(this.getKey())) {
criteria.put("$example", sample);
return this;
}
Criteria exampleCriteria = new Criteria();
exampleCriteria.criteria.put("$example", sample);
return registerCriteriaChainElement(exampleCriteria);
}
/**
* Creates a criterion ({@code $jsonSchema}) matching documents against a given structure defined by the
* {@link MongoJsonSchema}. <br />

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

@ -134,16 +134,14 @@ public class QueryByExampleTests { @@ -134,16 +134,14 @@ public class QueryByExampleTests {
assertThat(result).containsExactlyInAnyOrder(p1, p2, p3);
}
@Test // DATAMONGO-1245
@Test // DATAMONGO-1245, GH-3544
public void findByExampleWithCriteria() {
Person sample = new Person();
sample.lastname = "stark";
Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex("^ary*"));
List<Person> result = operations.find(query, Person.class);
assertThat(result).hasSize(1);
Query query = new Query(new Criteria().alike(Example.of(sample)).and("firstname").regex(".*n.*"));
assertThat(operations.find(query, Person.class)).containsExactly(p1);
}
@Test // DATAMONGO-1459

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

@ -772,6 +772,19 @@ public class QueryMapperUnitTests { @@ -772,6 +772,19 @@ public class QueryMapperUnitTests {
assertThat(document.get("legacyPoint.y")).isEqualTo(20D);
}
@Test // GH-3544
public void exampleWithCombinedCriteriaShouldBeMappedCorrectly() {
Foo probe = new Foo();
probe.embedded = new EmbeddedClass();
probe.embedded.id = "conflux";
Query query = query(byExample(probe).and("listOfItems").exists(true));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(), context.getPersistentEntity(Foo.class));
assertThat(document).containsEntry("embedded\\._id", "conflux").containsEntry("my_items", new org.bson.Document("$exists", true));
}
@Test // DATAMONGO-1988
public void mapsStringObjectIdRepresentationToObjectIdWhenReferencingIdProperty() {

Loading…
Cancel
Save