diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java index aacdb8dd9..e49bd2d65 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java @@ -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 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 void insertList(List listToSave, MongoWriter writer) { + if (null != mappingContext) { + Map> objs = new HashMap>(); + for (Object o : listToSave) { + PersistentEntity entity = mappingContext.getPersistentEntity(o.getClass()); + if (null != entity && entity instanceof MongoPersistentEntity) { + String coll = ((MongoPersistentEntity) entity).getCollection(); + List objList = objs.get(coll); + if (null == objList) { + objList = new ArrayList(); + objs.put(coll, objList); + } + objList.add(o); + } else { + continue; + } + } + for (Map.Entry> 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(); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/GeneratedId.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/GeneratedId.java new file mode 100644 index 000000000..6716c4ea4 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/GeneratedId.java @@ -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 + */ +@Document +public class GeneratedId { + + @Id + private ObjectId id; + private String name; + + public GeneratedId(String name) { + this.name = name; + } + + public ObjectId getId() { + return id; + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java index 4b8f0070f..de48e5bfe 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java @@ -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 @@ -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 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 persons = new ArrayList(); + persons.add(new PersonCustomCollection1(55555, "Person", "One")); + persons.add(new PersonCustomCollection2(66666, "Person", "Two")); + template.insertList(persons); + + List p1Results = template.find("person1", + new Query(Criteria.where("ssn").is(55555)), + PersonCustomCollection1.class); + List 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( diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomCollection1.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomCollection1.java new file mode 100644 index 000000000..0bde08634 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomCollection1.java @@ -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 + */ +@Document(collection = "person1") +public class PersonCustomCollection1 extends BasePerson{ + + public PersonCustomCollection1(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomCollection2.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomCollection2.java new file mode 100644 index 000000000..8dcfe6cc0 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomCollection2.java @@ -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 + */ +@Document(collection = "person2") +public class PersonCustomCollection2 extends BasePerson { + + public PersonCustomCollection2(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } + +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomIdName.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomIdName.java index 5fdcb4815..556abf66f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomIdName.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonCustomIdName.java @@ -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