DATADOC-137 - Repositories now correctly replace multiple placeholders in JSON based queries.
When annotation a repository method with e.g. @Query("{ 'firstname' : ?0, 'lastname' : ?1 }") all placeholders get now replaced correctly. Added unit tests and fixed broken logging in StringBasedQueryCreator,
This commit is contained in:
@@ -72,7 +72,7 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
|
||||
}
|
||||
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug(String.format("Created query {}", query.getQueryObject()));
|
||||
LOG.debug(String.format("Created query %s", query.getQueryObject()));
|
||||
}
|
||||
|
||||
return query;
|
||||
@@ -86,7 +86,7 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
|
||||
while (matcher.find()) {
|
||||
String group = matcher.group();
|
||||
int index = Integer.parseInt(matcher.group(1));
|
||||
result = input.replace(group, getParameterWithIndex(accessor, index));
|
||||
result = result.replace(group, getParameterWithIndex(accessor, index));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -92,6 +92,26 @@ public class StringBasedMongoQueryUnitTests {
|
||||
assertThat(query.getQueryObject(), is(reference.getQueryObject()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindsMultipleParametersCorrectly() throws SecurityException, NoSuchMethodException {
|
||||
|
||||
Method method = SampleRepository.class.getMethod("findByLastnameAndAddress", String.class, Address.class);
|
||||
MongoQueryMethod queryMethod = new MongoQueryMethod(method, metadata, creator);
|
||||
StringBasedMongoQuery mongoQuery = new StringBasedMongoQuery(queryMethod, template);
|
||||
|
||||
Address address = new Address("Foo", "0123", "Bar");
|
||||
ConvertingParameterAccessor accesor = StubParameterAccessor.getAccessor(converter, "Matthews", address);
|
||||
|
||||
DBObject addressDbObject = new BasicDBObject();
|
||||
converter.write(address, addressDbObject);
|
||||
|
||||
DBObject reference = new BasicDBObject("address", addressDbObject);
|
||||
reference.put("lastname", "Matthews");
|
||||
|
||||
org.springframework.data.document.mongodb.query.Query query = mongoQuery.createQuery(accesor);
|
||||
assertThat(query.getQueryObject(), is(reference));
|
||||
}
|
||||
|
||||
private interface SampleRepository {
|
||||
|
||||
@Query("{ 'lastname' : ?0 }")
|
||||
@@ -99,5 +119,8 @@ public class StringBasedMongoQueryUnitTests {
|
||||
|
||||
@Query("{ 'address' : ?0 }")
|
||||
Person findByAddress(Address address);
|
||||
|
||||
@Query("{ 'lastname' : ?0, 'address' : ?1 }")
|
||||
Person findByLastnameAndAddress(String lastname, Address address);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user