Change MongoTemplate.save to call getEntityCollection
MongoTemplate.getEntityCollection checks mappingContext to auto-register classes Remove autowire option from mapping-converter xsd Enable GeoSpatialTests
This commit is contained in:
@@ -90,7 +90,7 @@ public abstract class MongoDbUtils {
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.debug("Opening Mongo DB");
|
||||
LOGGER.trace("Getting Mongo Database name=["+databaseName+"]");
|
||||
DB db = mongo.getDB(databaseName);
|
||||
boolean credentialsGiven = username != null && password != null;
|
||||
|
||||
|
||||
@@ -668,7 +668,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#save(java.lang.Object)
|
||||
*/
|
||||
public void save(Object objectToSave) {
|
||||
save(getRequiredDefaultCollectionName(), objectToSave);
|
||||
save(getEntityCollection(objectToSave), objectToSave);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -1104,6 +1104,10 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
private <T> String getEntityCollection(Class<T> clazz) {
|
||||
if (null != mappingContext) {
|
||||
PersistentEntity<T> entity = mappingContext.getPersistentEntity(clazz);
|
||||
if (entity == null) {
|
||||
mappingContext.addPersistentEntity(clazz);
|
||||
entity = mappingContext.getPersistentEntity(clazz);
|
||||
}
|
||||
if (null != entity && entity instanceof MongoPersistentEntity) {
|
||||
return ((MongoPersistentEntity) entity).getCollection();
|
||||
}
|
||||
@@ -1321,5 +1325,9 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
public void setWriteResultChecking(WriteResultChecking resultChecking) {
|
||||
this.writeResultChecking = resultChecking;
|
||||
}
|
||||
|
||||
public void setWriteConcern(WriteConcern writeConcern) {
|
||||
this.writeConcern = writeConcern;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -120,13 +120,6 @@ The base package in which to scan for entities annotated with @Document
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="autowire" type="xsd:boolean">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
This controls whether or not to run entities through the BeanFactory's autowiring mechanism for using @Autowired and @Value on properties. Default is true.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="mongo-ref" type="mongoRef" use="optional">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
|
||||
@@ -21,6 +21,8 @@ import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@@ -42,15 +44,15 @@ import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoException;
|
||||
import com.mongodb.WriteConcern;
|
||||
/**
|
||||
* Modified from https://github.com/deftlabs/mongo-java-geospatial-example
|
||||
* @author Mark Pollack
|
||||
*
|
||||
*/
|
||||
//@RunWith(SpringJUnit4ClassRunner.class)
|
||||
//@ContextConfiguration("classpath:geospatial.xml")
|
||||
public class GeoSpatialTests {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(GeoSpatialTests.class);
|
||||
private final String[] collectionsToDrop = new String[]{"newyork"};
|
||||
|
||||
ApplicationContext applicationContext;
|
||||
@@ -65,12 +67,14 @@ public class GeoSpatialTests {
|
||||
}
|
||||
applicationContext = new ClassPathXmlApplicationContext("/geospatial.xml");
|
||||
template = applicationContext.getBean(MongoTemplate.class);
|
||||
//template.dropCollection(template.getDefaultCollectionName());
|
||||
template.setWriteConcern(WriteConcern.FSYNC_SAFE);
|
||||
template.ensureIndex(new GeospatialIndex("location"));
|
||||
indexCreated();
|
||||
addVenues();
|
||||
}
|
||||
|
||||
private void addVenues() {
|
||||
|
||||
template.insert(new Venue("Penn Station", -73.99408, 40.75057));
|
||||
template.insert(new Venue("10gen Office", -73.99171, 40.738868));
|
||||
template.insert(new Venue("Flatiron Building", -73.988135, 40.741404));
|
||||
@@ -102,15 +106,18 @@ public class GeoSpatialTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void indexCreated() {
|
||||
public void searchAllData() {
|
||||
assertThat(template, notNullValue());
|
||||
Venue foundVenue = template.findOne(
|
||||
new Query(Criteria.where("name").is("Penn Station")), Venue.class);
|
||||
assertThat(foundVenue, notNullValue());
|
||||
List<Venue> venues = template.getCollection(Venue.class);
|
||||
assertThat(venues.size(), equalTo(13));
|
||||
|
||||
}
|
||||
|
||||
public void indexCreated() {
|
||||
List<DBObject> indexInfo = getIndexInfo();
|
||||
LOGGER.debug(indexInfo);
|
||||
assertThat(indexInfo.size(), equalTo(2));
|
||||
assertThat(indexInfo.get(1).get("name").toString(), equalTo("location_2d"));
|
||||
assertThat(indexInfo.get(1).get("ns").toString(),
|
||||
|
||||
@@ -17,12 +17,14 @@ package org.springframework.data.document.mongodb;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
import org.springframework.data.document.mongodb.mapping.Document;
|
||||
|
||||
@Document
|
||||
@Document(collection="newyork")
|
||||
public class Venue {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
private Double[] location;
|
||||
@@ -47,6 +49,14 @@ public class Venue {
|
||||
public Double[] getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
|
||||
|
||||
<mongo:mongo host="localhost" port="27017"/>
|
||||
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.mapping"
|
||||
autowire="true"/>
|
||||
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.mapping"/>
|
||||
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo"/>
|
||||
|
||||
@@ -6,8 +6,7 @@
|
||||
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
|
||||
|
||||
<mongo:mongo host="localhost" port="27017"/>
|
||||
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.mapping"
|
||||
autowire="true"/>
|
||||
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.mapping"/>
|
||||
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
|
||||
<constructor-arg ref="mongo"/>
|
||||
|
||||
Reference in New Issue
Block a user