From 356e6acd431d33f6c0514148727d1324af357e91 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 24 Jul 2012 13:30:04 +0200 Subject: [PATCH] DATAMONGO-474 - Populating id's after save now inspects field only. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit So far the algorithm to inspect whether an id property has to be set after a save(…) operation has used the plain BeanWrapper.getProperty(PersistentProperty property) method. This caused problems in case the getter of the id field returned something completely different (to be precise: a complex type not convertible out of the box). We now inspect the id field only to retrieve the value. --- .../data/mongodb/core/MongoTemplate.java | 2 +- .../mongodb/core/MongoTemplateUnitTests.java | 44 ++++++++++++++++--- 2 files changed, 39 insertions(+), 7 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 e427962cc..d4e9d4c7d 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 @@ -1362,7 +1362,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware { try { - Object idValue = wrapper.getProperty(idProp); + Object idValue = wrapper.getProperty(idProp, idProp.getType(), true); if (idValue != null) { return; diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java index 42853ab72..23413fbfa 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java @@ -20,6 +20,7 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.*; import java.math.BigInteger; +import java.util.regex.Pattern; import org.bson.types.ObjectId; import org.junit.Before; @@ -136,6 +137,31 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { assertThat(entity.id, is(notNullValue())); } + /** + * @see DATAMONGO-474 + */ + @Test + public void setsUnpopulatedIdField() { + + NotAutogenerateableId entity = new NotAutogenerateableId(); + + template.populateIdIfNecessary(entity, 5); + assertThat(entity.id, is(5)); + } + + /** + * @see DATAMONGO-474 + */ + @Test + public void doesNotSetAlreadyPopulatedId() { + + NotAutogenerateableId entity = new NotAutogenerateableId(); + entity.id = 5; + + template.populateIdIfNecessary(entity, 7); + assertThat(entity.id, is(5)); + } + class AutogenerateableId { @Id @@ -146,6 +172,10 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { @Id Integer id; + + public Pattern getId() { + return Pattern.compile("."); + } } /** @@ -161,9 +191,10 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { return template; } - /* (non-Javadoc) - * @see org.springframework.data.mongodb.core.core.MongoOperationsUnitTests#getOperations() - */ + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.MongoOperationsUnitTests#getOperationsForExceptionHandling() + */ @Override protected MongoOperations getOperationsForExceptionHandling() { MongoTemplate template = spy(this.template); @@ -171,9 +202,10 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { return template; } - /* (non-Javadoc) - * @see org.springframework.data.mongodb.core.core.MongoOperationsUnitTests#getOperations() - */ + /* + * (non-Javadoc) + * @see org.springframework.data.mongodb.core.MongoOperationsUnitTests#getOperations() + */ @Override protected MongoOperations getOperations() { return this.template;