Added code to look for @Document(collection="customcollection") in template.insertList(), added tests for same.
This commit is contained in:
@@ -18,9 +18,12 @@ package org.springframework.data.document.mongodb;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
@@ -59,9 +62,12 @@ import org.springframework.data.document.mongodb.mapping.event.BeforeSaveEvent;
|
||||
import org.springframework.data.document.mongodb.mapping.event.MongoMappingEvent;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.document.mongodb.query.Update;
|
||||
import org.springframework.data.mapping.MappingBeanHelper;
|
||||
import org.springframework.data.mapping.context.MappingContextAware;
|
||||
import org.springframework.data.mapping.model.MappingContext;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
import org.springframework.data.mapping.model.PersistentEntity;
|
||||
import org.springframework.data.mapping.model.PersistentProperty;
|
||||
import org.springframework.jca.cci.core.ConnectionCallback;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -606,7 +612,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List)
|
||||
*/
|
||||
public void insertList(List<? extends Object> listToSave) {
|
||||
insertList(getRequiredDefaultCollectionName(), listToSave);
|
||||
insertList(listToSave, mongoConverter);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -620,6 +626,28 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
* @see org.springframework.data.document.mongodb.MongoOperations#insertList(java.util.List, org.springframework.data.document.mongodb.MongoWriter)
|
||||
*/
|
||||
public <T> void insertList(List<? extends T> listToSave, MongoWriter<T> writer) {
|
||||
if (null != mappingContext) {
|
||||
Map<String, List<Object>> objs = new HashMap<String, List<Object>>();
|
||||
for (Object o : listToSave) {
|
||||
PersistentEntity<?> entity = mappingContext.getPersistentEntity(o.getClass());
|
||||
if (null != entity && entity instanceof MongoPersistentEntity) {
|
||||
String coll = ((MongoPersistentEntity) entity).getCollection();
|
||||
List<Object> objList = objs.get(coll);
|
||||
if (null == objList) {
|
||||
objList = new ArrayList<Object>();
|
||||
objs.put(coll, objList);
|
||||
}
|
||||
objList.add(o);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
for (Map.Entry<String, List<Object>> entry : objs.entrySet()) {
|
||||
insertList(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
insertList(getDefaultCollectionName(), listToSave, writer);
|
||||
}
|
||||
|
||||
@@ -998,6 +1026,23 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
|
||||
return;
|
||||
}
|
||||
|
||||
if (null != mappingContext) {
|
||||
PersistentEntity<?> entity = mappingContext.getPersistentEntity(savedObject.getClass());
|
||||
if (null != entity) {
|
||||
PersistentProperty idProp = entity.getIdProperty();
|
||||
if (null != idProp) {
|
||||
try {
|
||||
MappingBeanHelper.setProperty(savedObject, idProp, id);
|
||||
return;
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new MappingException(e.getMessage(), e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new MappingException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ConfigurablePropertyAccessor bw = PropertyAccessorFactory.forDirectFieldAccess(savedObject);
|
||||
MongoPropertyDescriptor idDescriptor = new MongoPropertyDescriptors(savedObject.getClass()).getIdDescriptor();
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.document.mongodb.mapping;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Document
|
||||
public class GeneratedId {
|
||||
|
||||
@Id
|
||||
private ObjectId id;
|
||||
private String name;
|
||||
|
||||
public GeneratedId(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ObjectId getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,6 @@ import org.springframework.data.document.mongodb.MongoDbUtils;
|
||||
import org.springframework.data.document.mongodb.MongoTemplate;
|
||||
import org.springframework.data.document.mongodb.query.Criteria;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.mapping.model.MappingException;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
@@ -44,7 +43,15 @@ import org.springframework.data.mapping.model.MappingException;
|
||||
public class MappingTests {
|
||||
|
||||
private static final Log LOGGER = LogFactory.getLog(MongoDbUtils.class);
|
||||
private final String[] collectionsToDrop = new String[]{"person", "personmapproperty", "personpojo", "personcustomidname", "account"};
|
||||
private final String[] collectionsToDrop = new String[]{
|
||||
"person",
|
||||
"personmapproperty",
|
||||
"personpojo",
|
||||
"personcustomidname",
|
||||
"person1",
|
||||
"person2",
|
||||
"account"
|
||||
};
|
||||
|
||||
ApplicationContext applicationContext;
|
||||
MongoTemplate template;
|
||||
@@ -62,6 +69,14 @@ public class MappingTests {
|
||||
mappingContext = applicationContext.getBean(MongoMappingContext.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneratedId() {
|
||||
GeneratedId genId = new GeneratedId("test");
|
||||
template.insert(genId);
|
||||
|
||||
assertNotNull(genId.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersonPojo() throws Exception {
|
||||
// POJOs aren't auto-detected, have to add manually
|
||||
@@ -84,12 +99,12 @@ public class MappingTests {
|
||||
// POJOs aren't auto-detected, have to add manually
|
||||
mappingContext.addPersistentEntity(PersonCustomIdName.class);
|
||||
|
||||
PersonCustomIdName p = new PersonCustomIdName(123456, "Custom Id");
|
||||
PersonCustomIdName p = new PersonCustomIdName(123456, "Custom Id", "LastName");
|
||||
template.insert(p);
|
||||
|
||||
List<PersonCustomIdName> result = template.find(new Query(Criteria.where("ssn").is(123456)), PersonCustomIdName.class);
|
||||
assertThat(result.size(), is(1));
|
||||
assertNotNull(result.get(0).getLastName());
|
||||
assertThat(result.get(0).getLastName(), is("LastName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -169,6 +184,23 @@ public class MappingTests {
|
||||
assertThat(result.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomCollectionInList() {
|
||||
List<BasePerson> persons = new ArrayList<BasePerson>();
|
||||
persons.add(new PersonCustomCollection1(55555, "Person", "One"));
|
||||
persons.add(new PersonCustomCollection2(66666, "Person", "Two"));
|
||||
template.insertList(persons);
|
||||
|
||||
List<PersonCustomCollection1> p1Results = template.find("person1",
|
||||
new Query(Criteria.where("ssn").is(55555)),
|
||||
PersonCustomCollection1.class);
|
||||
List<PersonCustomCollection2> p2Results = template.find("person2",
|
||||
new Query(Criteria.where("ssn").is(66666)),
|
||||
PersonCustomCollection2.class);
|
||||
assertThat(p1Results.size(), is(1));
|
||||
assertThat(p2Results.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrimitivesAndCustomCollectionName() {
|
||||
Location loc = new Location(
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.document.mongodb.mapping;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Document(collection = "person1")
|
||||
public class PersonCustomCollection1 extends BasePerson{
|
||||
|
||||
public PersonCustomCollection1(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.data.document.mongodb.mapping;
|
||||
|
||||
/**
|
||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||
*/
|
||||
@Document(collection = "person2")
|
||||
public class PersonCustomCollection2 extends BasePerson {
|
||||
|
||||
public PersonCustomCollection2(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,9 +26,10 @@ public class PersonCustomIdName extends BasePerson {
|
||||
@Id
|
||||
private String lastName;
|
||||
|
||||
public PersonCustomIdName(Integer ssn, String firstName) {
|
||||
public PersonCustomIdName(Integer ssn, String firstName, String lastName) {
|
||||
this.ssn = ssn;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user