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-04-02 18:00:59 +02:00
parent 9421c45c5a
commit 3be35cba2d
6 changed files with 51 additions and 23 deletions

View File

@@ -20,11 +20,12 @@ import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
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

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,6 +32,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.geo.Box;
import org.springframework.data.mongodb.core.geo.Circle;
import org.springframework.data.mongodb.core.geo.Distance;
@@ -53,19 +54,24 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Autowired
protected PersonRepository repository;
@Autowired
MongoOperations operations;
Person dave, oliver, carter, boyd, stefan, leroi, alicia;
QPerson person;
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);
@@ -396,4 +402,24 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
assertThat(result.get(5), is(oliver));
assertThat(result.get(6), is(stefan));
}
/**
* @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;
@@ -41,6 +42,7 @@ public class Person extends Contact {
private Integer age;
@SuppressWarnings("unused")
private Sex sex;
Date createdAt;
@GeoSpatialIndexed
private Point location;
@@ -71,6 +73,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;
@@ -151,4 +152,15 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
GeoResults<Person> findByLocationNear(Point point, Distance maxDistance);
GeoPage<Person> findByLocationNear(Point point, Distance maxDistance, Pageable pageable);
/**
* @see DATAMONGO-425
*/
List<Person> findByCreatedAtLessThan(Date date);
/**
* @see DATAMONGO-425
*/
@Query("{ 'createdAt' : { '$lt' : ?0 }}")
List<Person> findByCreatedAtLessThanManually(Date date);
}

View File

@@ -77,7 +77,7 @@ public class RepositoryIndexCreationIntegrationTests {
assertThat(indexInfo.isEmpty(), is(false));
assertThat(indexInfo.size(), is(greaterThan(2)));
assertThat(getIndexNamesFrom(indexInfo), hasItems("findByLastname", "findByFirstnameNotIn"));
assertThat(getIndexNamesFrom(indexInfo), hasItems("findByLastnameLike", "findByFirstnameLike"));
return null;
}

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);