DATAMONGO-234 - MongoTemplate should support the findAndModify operation to update version fields

This commit is contained in:
Mark Pollack
2011-12-06 00:26:18 -05:00
parent 21010fbd49
commit ef6e08d3f4
3 changed files with 59 additions and 6 deletions

View File

@@ -521,18 +521,15 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
public <T> T findAndModify(Query query, Update update, Class<T> entityClass) {
// TODO Auto-generated method stub
return null;
return findAndModify(query, update, new FindAndModifyOptions(), entityClass, determineCollectionName(entityClass));
}
public <T> T findAndModify(Query query, Update update, Class<T> entityClass, String collectionName) {
// TODO Auto-generated method stub
return null;
return findAndModify(query, update, new FindAndModifyOptions(), entityClass, collectionName);
}
public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass) {
// TODO Auto-generated method stub
return null;
return findAndModify(query, update, options, entityClass, determineCollectionName(entityClass));
}
public <T> T findAndModify(Query query, Update update, FindAndModifyOptions options, Class<T> entityClass,
@@ -1304,6 +1301,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
Class<T> entityClass, Update update, FindAndModifyOptions options) {
EntityReader<? super T, DBObject> readerToUse = this.mongoConverter;
if (options == null) {
options = new FindAndModifyOptions();
}
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);

View File

@@ -391,6 +391,47 @@ public class MongoTemplateTests {
private void checkCollectionContents(Class<?> entityClass, int count) {
assertThat(template.findAll(entityClass).size(), is(count));
}
@Test
public void testFindAndUpdate() {
template.insert(new Person("Tom", 21));
template.insert(new Person("Dick", 22));
template.insert(new Person("Harry", 23));
Query query = new Query(Criteria.where("firstName").is("Harry"));
Update update = new Update().inc("age", 1);
Person p = template.findAndModify(query, update, Person.class); // return old
assertThat(p.getFirstName(), is("Harry"));
assertThat(p.getAge(), is(23));
p = template.findOne(query, Person.class);
assertThat(p.getAge(), is(24));
p = template.findAndModify(query, update, Person.class, "person");
assertThat(p.getAge(), is(24));
p = template.findOne(query, Person.class);
assertThat(p.getAge(), is(25));
p = template.findAndModify(query, update, new FindAndModifyOptions().returnNew(true), Person.class);
assertThat(p.getAge(), is(26));
p = template.findAndModify(query, update, null, Person.class, "person");
assertThat(p.getAge(), is(26));
p = template.findOne(query, Person.class);
assertThat(p.getAge(), is(27));
}
@Test
public void testFindAndUpdateUpsert() {
template.insert(new Person("Tom", 21));
template.insert(new Person("Dick", 22));
Query query = new Query(Criteria.where("firstName").is("Harry"));
Update update = new Update().set("age", 23);
Person p = template.findAndModify(query, update, new FindAndModifyOptions().upsert(true).returnNew(true), Person.class);
assertThat(p.getFirstName(), is("Harry"));
assertThat(p.getAge(), is(23));
}
@Test
public void testFindAndRemove() throws Exception {

View File

@@ -31,10 +31,21 @@ public class Person {
this.id = new ObjectId();
}
@Override
public String toString() {
return "Person [id=" + id + ", firstName=" + firstName + ", age=" + age + ", friend=" + friend + "]";
}
public Person(ObjectId id, String firstname) {
this.id = id;
this.firstName = firstname;
}
public Person(String firstname, int age) {
this();
this.firstName = firstname;
this.age = age;
}
public Person(String firstname) {
this();