From 6139e83d8d85ad2f423685fa6613afd1701ecbdf Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 26 Feb 2013 10:52:10 +0100 Subject: [PATCH] DATAMONGO-620 - Updating versioned object uses explicit collection name. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We're now handing the collection name given to doSaveVersioned(…) to the update method. --- .../data/mongodb/core/MongoTemplate.java | 6 +++--- .../data/mongodb/core/MongoTemplateTests.java | 20 ++++++++++++++++++- ...ersonWithVersionPropertyOfTypeInteger.java | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) 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 ca39e885d..dbb9510dc 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 @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 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. @@ -792,7 +792,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { maybeEmitEvent(new BeforeSaveEvent(objectToSave, dbObject)); Update update = Update.fromDBObject(dbObject, ID_FIELD); - updateFirst(query, update, objectToSave.getClass()); + doUpdate(collectionName, query, update, objectToSave.getClass(), false, false); maybeEmitEvent(new AfterSaveEvent(objectToSave, dbObject)); } } @@ -941,7 +941,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { if (entity != null && entity.hasVersionProperty() && !multi) { if (writeResult.getN() == 0) { throw new OptimisticLockingFailureException("Optimistic lock exception on saving entity: " - + updateObj.toMap().toString()); + + updateObj.toMap().toString() + " to collection " + collectionName); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java index 9be0f338b..81d4413fc 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 the original author or authors. + * Copyright 2011-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. @@ -146,6 +146,7 @@ public class MongoTemplateTests { template.dropCollection(Sample.class); template.dropCollection(MyPerson.class); template.dropCollection("collection"); + template.dropCollection("personX"); } @Test @@ -949,6 +950,7 @@ public class MongoTemplateTests { } @Test + @SuppressWarnings("deprecation") public void testUsingReadPreference() throws Exception { this.template.execute("readPref", new CollectionCallback() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { @@ -1467,6 +1469,22 @@ public class MongoTemplateTests { assertThat(template.find(null, PersonWithIdPropertyOfTypeObjectId.class), is(result)); } + /** + * @see DATAMONGO-620 + */ + @Test + public void versionsObjectIntoDedicatedCollection() { + + PersonWithVersionPropertyOfTypeInteger person = new PersonWithVersionPropertyOfTypeInteger(); + person.firstName = "Dave"; + + template.save(person, "personX"); + assertThat(person.version, is(0)); + + template.save(person, "personX"); + assertThat(person.version, is(1)); + } + static class MyId { String first; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeInteger.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeInteger.java index c6e6e52d3..aef61e471 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeInteger.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/PersonWithVersionPropertyOfTypeInteger.java @@ -15,7 +15,7 @@ */ package org.springframework.data.mongodb.core; -import org.springframework.data.mongodb.core.mapping.Version; +import org.springframework.data.annotation.Version; public class PersonWithVersionPropertyOfTypeInteger {