From 190d7cefb0c31c909655255ea133ff72bf67d5c0 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 24 Jul 2012 13:23:40 +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 | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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 9021d3c61..b4d076420 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 @@ -1356,7 +1356,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 59db40938..b972c24e0 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 @@ -22,6 +22,7 @@ import static org.mockito.Mockito.*; import java.math.BigInteger; import java.util.Collections; +import java.util.regex.Pattern; import org.bson.types.ObjectId; import org.junit.Before; @@ -172,6 +173,31 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { verify(collection, times(1)).update(Mockito.any(DBObject.class), eq(reference), anyBoolean(), anyBoolean()); } + /** + * @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 @@ -182,6 +208,10 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests { @Id Integer id; + + public Pattern getId() { + return Pattern.compile("."); + } } enum MyConverter implements Converter {