diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java index 0b2dea58e..edc74c331 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2011 the original author or authors. + * Copyright 2010-2013 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. @@ -408,7 +408,6 @@ public interface MongoOperations { * specification * @param entityClass the parameterized type of the returned list. * @param collectionName name of the collection to retrieve the objects from - * * @return the converted object */ T findOne(Query query, Class entityClass, String collectionName); @@ -442,7 +441,6 @@ public interface MongoOperations { * specification * @param entityClass the parameterized type of the returned list. * @param collectionName name of the collection to retrieve the objects from - * * @return the List of converted objects */ List find(Query query, Class entityClass, String collectionName); @@ -464,7 +462,6 @@ public interface MongoOperations { * @param id the id of the document to return * @param entityClass the type to convert the document to * @param collectionName the collection to query for the document - * * @param * @return */ @@ -510,7 +507,6 @@ public interface MongoOperations { * specification * @param entityClass the parameterized type of the returned list. * @param collectionName name of the collection to retrieve the objects from - * * @return the converted object */ T findAndRemove(Query query, Class entityClass, String collectionName); @@ -713,11 +709,12 @@ public interface MongoOperations { * Remove all documents that match the provided query document criteria from the the collection used to store the * entityClass. The Class parameter is also used to help convert the Id of the object if it is present in the query. * - * @param * @param query * @param entityClass */ - void remove(Query query, Class entityClass); + void remove(Query query, Class entityClass); + + void remove(Query query, Class entityClass, String collectionName); /** * Remove all documents from the specified collection that match the provided query document criteria. There is no @@ -734,4 +731,4 @@ public interface MongoOperations { * @return */ MongoConverter getConverter(); -} \ No newline at end of file +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 37e4f9831..cdfe7ad63 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -1015,17 +1015,26 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { } } - public void remove(Query query, Class entityClass) { - Assert.notNull(query); - doRemove(determineCollectionName(entityClass), query, entityClass); + public void remove(Query query, String collectionName) { + remove(query, null, collectionName); + } + + public void remove(Query query, Class entityClass) { + remove(query, entityClass, determineCollectionName(entityClass)); + } + + public void remove(Query query, Class entityClass, String collectionName) { + doRemove(collectionName, query, entityClass); } protected void doRemove(final String collectionName, final Query query, final Class entityClass) { if (query == null) { - throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null"); + throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null!"); } + Assert.hasText(collectionName, "Collection name must not be null or empty!"); + final DBObject queryObject = query.getQueryObject(); final MongoPersistentEntity entity = getPersistentEntity(entityClass); @@ -1055,10 +1064,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { }); } - public void remove(final Query query, String collectionName) { - doRemove(collectionName, query, null); - } - public List findAll(Class entityClass) { return executeFindMultiInternal(new FindCallback(null), null, new ReadDbObjectCallback(mongoConverter, entityClass), determineCollectionName(entityClass)); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java index 804b98cc0..c5f7805f1 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java @@ -96,7 +96,7 @@ public class SimpleMongoRepository implements MongoR */ public T findOne(ID id) { Assert.notNull(id, "The given id must not be null!"); - return mongoOperations.findById(id, entityInformation.getJavaType()); + return mongoOperations.findById(id, entityInformation.getJavaType(), entityInformation.getCollectionName()); } private Query getIdQuery(Object id) { @@ -136,7 +136,7 @@ public class SimpleMongoRepository implements MongoR */ public void delete(ID id) { Assert.notNull(id, "The given id must not be null!"); - mongoOperations.remove(getIdQuery(id), entityInformation.getJavaType()); + mongoOperations.remove(getIdQuery(id), entityInformation.getJavaType(), entityInformation.getCollectionName()); } /* diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java new file mode 100755 index 000000000..4c3600d67 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java @@ -0,0 +1,130 @@ +/* + * Copyright 2010-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. + * 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.mongodb.repository.support; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.repository.Person; +import org.springframework.data.mongodb.repository.Person.Sex; +import org.springframework.data.mongodb.repository.query.MongoEntityInformation; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author A. B. M. Kowser + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:infrastructure.xml") +public class SimpleMongoRepositoryTests { + + @Autowired + private MongoTemplate template; + + private Person oliver, dave, carter, boyd, stefan, leroi, alicia; + private List all; + + private MongoEntityInformation personEntityInformation = new CustomizedPersonInformation(); + private SimpleMongoRepository repository; + + @Before + public void setUp() { + repository = new SimpleMongoRepository(personEntityInformation, template); + repository.deleteAll(); + + oliver = new Person("Oliver August", "Matthews", 4); + dave = new Person("Dave", "Matthews", 42); + carter = new Person("Carter", "Beauford", 49); + boyd = new Person("Boyd", "Tinsley", 45); + stefan = new Person("Stefan", "Lessard", 34); + leroi = new Person("Leroi", "Moore", 41); + alicia = new Person("Alicia", "Keys", 30, Sex.FEMALE); + + all = repository.save(Arrays.asList(oliver, dave, carter, boyd, stefan, leroi, alicia)); + } + + @Test + public void findALlFromCustomCollectionName() { + List result = repository.findAll(); + assertThat(result, hasSize(all.size())); + } + + @Test + public void findOneFromCustomCollectionName() { + Person result = repository.findOne(dave.getId()); + assertThat(result, is(dave)); + } + + @Test + public void deleteFromCustomCollectionName() { + repository.delete(dave); + List result = repository.findAll(); + + assertThat(result, hasSize(all.size() - 1)); + assertThat(result, not(hasItem(dave))); + } + + @Test + public void deleteByIdFromCustomCollectionName() { + repository.delete(dave.getId()); + List result = repository.findAll(); + + assertThat(result, hasSize(all.size() - 1)); + assertThat(result, not(hasItem(dave))); + } + + private static class CustomizedPersonInformation implements MongoEntityInformation { + + @Override + public boolean isNew(Person entity) { + return entity.getId() == null; + } + + @Override + public String getId(Person entity) { + return entity.getId(); + } + + @Override + public Class getIdType() { + return String.class; + } + + @Override + public Class getJavaType() { + return Person.class; + } + + @Override + public String getCollectionName() { + return "customizedPerson"; + } + + @Override + public String getIdAttribute() { + return "id"; + } + } + +}