DATAMONGO-425 - Fixed parameter binding for Dates and manually defined queries.

Replaced manual JSON serialization for special parameters inside StringBasedMongoQuery by calling JSON.serialize(…).
This commit is contained in:
Oliver Gierke
2012-03-31 18:44:58 -06:00
parent 6373a70ca4
commit 3dcf93744b
5 changed files with 44 additions and 21 deletions

View File

@@ -18,13 +18,14 @@ package org.springframework.data.mongodb.repository.query;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Query;
import com.mongodb.util.JSON;
/**
* Query to use a plain JSON String to create the {@link Query} to actually execute.
*
@@ -55,12 +56,9 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.mongodb.repository.AbstractMongoQuery#createQuery(org.springframework.data.
* repository.query.SimpleParameterAccessor, org.springframework.data.mongodb.core.core.support.convert.MongoConverter)
*/
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.query.AbstractMongoQuery#createQuery(org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor)
*/
@Override
protected Query createQuery(ConvertingParameterAccessor accessor) {
@@ -99,17 +97,6 @@ public class StringBasedMongoQuery extends AbstractMongoQuery {
}
private String getParameterWithIndex(ConvertingParameterAccessor accessor, int index) {
Object parameter = accessor.getBindableValue(index);
if (parameter == null) {
return "null";
} else if (parameter instanceof String || parameter.getClass().isEnum()) {
return String.format("\"%s\"", parameter);
} else if (parameter instanceof ObjectId) {
return String.format("{ '$oid' : '%s' }", parameter);
}
return parameter.toString();
return JSON.serialize(accessor.getBindableValue(index));
}
}

View File

@@ -64,13 +64,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
List<Person> all;
@Before
public void setUp() {
public void setUp() throws InterruptedException {
repository.deleteAll();
dave = new Person("Dave", "Matthews", 42);
oliver = new Person("Oliver August", "Matthews", 4);
carter = new Person("Carter", "Beauford", 49);
Thread.sleep(10);
boyd = new Person("Boyd", "Tinsley", 45);
stefan = new Person("Stefan", "Lessard", 34);
leroi = new Person("Leroi", "Moore", 41);
@@ -430,4 +431,24 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
assertThat(result.size(), is(1));
assertThat(result, hasItem(dave));
}
/**
* @see DATAMONGO-425
*/
@Test
public void bindsDateParameterForDerivedQueryCorrectly() {
List<Person> result = repository.findByCreatedAtLessThan(boyd.createdAt);
assertThat(result.isEmpty(), is(false));
}
/**
* @see DATAMONGO-425
*/
@Test
public void bindsDateParameterForManuallyDefinedQueryCorrectly() {
List<Person> result = repository.findByCreatedAtLessThanManually(boyd.createdAt);
assertThat(result.isEmpty(), is(false));
}
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.repository;
import java.util.Date;
import java.util.Set;
import org.springframework.data.mongodb.core.geo.Point;
@@ -42,6 +43,7 @@ public class Person extends Contact {
private Integer age;
@SuppressWarnings("unused")
private Sex sex;
Date createdAt;
@GeoSpatialIndexed
private Point location;
@@ -75,6 +77,7 @@ public class Person extends Contact {
this.age = age;
this.sex = sex;
this.email = (firstname == null ? "noone" : firstname.toLowerCase()) + "@dmband.com";
this.createdAt = new Date();
}
/**

View File

@@ -16,6 +16,7 @@
package org.springframework.data.mongodb.repository;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.springframework.data.domain.Page;
@@ -153,4 +154,15 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
GeoPage<Person> findByLocationNear(Point point, Distance maxDistance, Pageable pageable);
List<Person> findByCreator(User user);
/**
* @see DATAMONGO-425
*/
List<Person> findByCreatedAtLessThan(Date date);
/**
* @see DATAMONGO-425
*/
@Query("{ 'createdAt' : { '$lt' : ?0 }}")
List<Person> findByCreatedAtLessThanManually(Date date);
}

View File

@@ -26,7 +26,7 @@ public class MongoNamespaceIntegrationTests extends AbstractPersonRepositoryInte
@Before
@Override
public void setUp() {
public void setUp() throws InterruptedException {
super.setUp();
factory = new DefaultListableBeanFactory();
reader = new XmlBeanDefinitionReader(factory);