Adapted newly introduced Property model.
Added integration tests for traversing nested properties. Added query logging at debug level.
This commit is contained in:
@@ -17,6 +17,8 @@ package org.springframework.data.document.mongodb.repository;
|
|||||||
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.data.document.mongodb.MongoConverter;
|
import org.springframework.data.document.mongodb.MongoConverter;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.repository.query.SimpleParameterAccessor;
|
import org.springframework.data.repository.query.SimpleParameterAccessor;
|
||||||
@@ -38,6 +40,7 @@ import com.mongodb.QueryBuilder;
|
|||||||
*/
|
*/
|
||||||
class MongoQueryCreator extends AbstractQueryCreator<DBObject, QueryBuilder> {
|
class MongoQueryCreator extends AbstractQueryCreator<DBObject, QueryBuilder> {
|
||||||
|
|
||||||
|
private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class);
|
||||||
private final MongoConverter converter;
|
private final MongoConverter converter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,7 +69,7 @@ class MongoQueryCreator extends AbstractQueryCreator<DBObject, QueryBuilder> {
|
|||||||
@Override
|
@Override
|
||||||
protected QueryBuilder create(Part part, BindableParameterIterator iterator) {
|
protected QueryBuilder create(Part part, BindableParameterIterator iterator) {
|
||||||
|
|
||||||
return from(part.getType(), QueryBuilder.start(part.getProperty()),
|
return from(part.getType(), QueryBuilder.start(part.getProperty().toDotPath()),
|
||||||
iterator);
|
iterator);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +87,7 @@ class MongoQueryCreator extends AbstractQueryCreator<DBObject, QueryBuilder> {
|
|||||||
protected QueryBuilder and(Part part, QueryBuilder base,
|
protected QueryBuilder and(Part part, QueryBuilder base,
|
||||||
BindableParameterIterator iterator) {
|
BindableParameterIterator iterator) {
|
||||||
|
|
||||||
return from(part.getType(), base.and(part.getProperty()), iterator);
|
return from(part.getType(), base.and(part.getProperty().toDotPath()), iterator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -113,7 +116,13 @@ class MongoQueryCreator extends AbstractQueryCreator<DBObject, QueryBuilder> {
|
|||||||
@Override
|
@Override
|
||||||
protected DBObject complete(QueryBuilder criteria, Sort sort) {
|
protected DBObject complete(QueryBuilder criteria, Sort sort) {
|
||||||
|
|
||||||
return criteria.get();
|
DBObject query = criteria.get();
|
||||||
|
|
||||||
|
if (LOG.isDebugEnabled()) {
|
||||||
|
LOG.debug("Created query " + query);
|
||||||
|
}
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -119,4 +119,16 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
|
|||||||
assertThat(result.size(), is(1));
|
assertThat(result.size(), is(1));
|
||||||
assertThat(result, hasItem(dave));
|
assertThat(result, hasItem(dave));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findsPeopleByZipCode() throws Exception {
|
||||||
|
|
||||||
|
Address address = new Address("Foo Street 1", "C0123", "Bar");
|
||||||
|
dave.setAddress(address);
|
||||||
|
repository.save(dave);
|
||||||
|
|
||||||
|
List<Person> result = repository.findByAddressZipCode(address.getZipCode());
|
||||||
|
assertThat(result.size(), is(1));
|
||||||
|
assertThat(result, hasItem(dave));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,7 @@ public class MongoQueryCreatorUnitTests {
|
|||||||
@Test
|
@Test
|
||||||
public void createsQueryCorrectly() throws Exception {
|
public void createsQueryCorrectly() throws Exception {
|
||||||
|
|
||||||
PartTree tree = new PartTree("findByFirstname", Person.class);
|
PartTree tree = new PartTree("findByFirstName", Person.class);
|
||||||
|
|
||||||
MongoQueryCreator creator =
|
MongoQueryCreator creator =
|
||||||
new MongoQueryCreator(tree, new SimpleParameterAccessor(
|
new MongoQueryCreator(tree, new SimpleParameterAccessor(
|
||||||
@@ -70,7 +70,7 @@ public class MongoQueryCreatorUnitTests {
|
|||||||
creator.createQuery();
|
creator.createQuery();
|
||||||
|
|
||||||
creator =
|
creator =
|
||||||
new MongoQueryCreator(new PartTree("findByFirstnameAndFriend",
|
new MongoQueryCreator(new PartTree("findByFirstNameAndFriend",
|
||||||
Person.class), new SimpleParameterAccessor(
|
Person.class), new SimpleParameterAccessor(
|
||||||
new Parameters(findByFirstnameAndFriend), new Object[] {
|
new Parameters(findByFirstnameAndFriend), new Object[] {
|
||||||
"Oliver", new Person() }), converter);
|
"Oliver", new Person() }), converter);
|
||||||
|
|||||||
@@ -84,4 +84,7 @@ public interface PersonRepository extends MongoRepository<Person, String> {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<Person> findByAddress(Address address);
|
List<Person> findByAddress(Address address);
|
||||||
|
|
||||||
|
|
||||||
|
List<Person> findByAddressZipCode(String zipCode);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user