Browse Source

DATAMONGO-1360 - Query instances contained in a Near Query now get mapped during geoNear(…) execution.

A Query instance which might be part of a NearQuery definition is now passed through the QueryMapper to make sure complex types contained in it or even in more general types that have custom conversions registered are mapped correctly before the near command is actually executed.
pull/342/head
Oliver Gierke 10 years ago
parent
commit
b4753f3a83
  1. 9
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  2. 13
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/Venue.java
  3. 13
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java

9
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

@ -654,8 +654,15 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
} }
String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass); String collection = StringUtils.hasText(collectionName) ? collectionName : determineCollectionName(entityClass);
DBObject nearDbObject = near.toDBObject();
BasicDBObject command = new BasicDBObject("geoNear", collection); BasicDBObject command = new BasicDBObject("geoNear", collection);
command.putAll(near.toDBObject()); command.putAll(nearDbObject);
if (nearDbObject.containsField("query")) {
DBObject query = (DBObject) nearDbObject.get("query");
command.put("query", queryMapper.getMappedObject(query, getPersistentEntity(entityClass)));
}
CommandResult commandResult = executeCommand(command, this.readPreference); CommandResult commandResult = executeCommand(command, this.readPreference);
List<Object> results = (List<Object>) commandResult.get("results"); List<Object> results = (List<Object>) commandResult.get("results");

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

@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core;
import java.util.Arrays; import java.util.Arrays;
import org.joda.time.LocalDate;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
@ -24,10 +25,10 @@ import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "newyork") @Document(collection = "newyork")
public class Venue { public class Venue {
@Id @Id private String id;
private String id;
private String name; private String name;
private double[] location; private double[] location;
private LocalDate openingDate;
@PersistenceConstructor @PersistenceConstructor
Venue(String name, double[] location) { Venue(String name, double[] location) {
@ -50,6 +51,14 @@ public class Venue {
return location; return location;
} }
public LocalDate getOpeningDate() {
return openingDate;
}
public void setOpeningDate(LocalDate openingDate) {
this.openingDate = openingDate;
}
@Override @Override
public String toString() { public String toString() {
return "Venue [id=" + id + ", name=" + name + ", location=" + Arrays.toString(location) + "]"; return "Venue [id=" + id + ", name=" + name + ", location=" + Arrays.toString(location) + "]";

13
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/AbstractGeoSpatialTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2015 the original author or authors. * Copyright 2015-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import static org.springframework.data.mongodb.core.query.Query.*;
import java.util.List; import java.util.List;
import org.joda.time.LocalDate;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -49,6 +50,7 @@ import com.mongodb.WriteConcern;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
* @author Oliver Gierke
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration @ContextConfiguration
@ -173,4 +175,13 @@ public abstract class AbstractGeoSpatialTests {
assertThat(venues.size(), is(11)); assertThat(venues.size(), is(11));
} }
/**
* @see DATAMONGO-1360
*/
@Test
public void mapsQueryContainedInNearQuery() {
Query query = query(where("openingDate").lt(LocalDate.now()));
template.geoNear(NearQuery.near(1.5, 1.7).query(query), Venue.class);
}
} }

Loading…
Cancel
Save