DATAMONGO-474 - Populating id's after save now inspects field only.

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.
This commit is contained in:
Oliver Gierke
2012-07-24 13:23:40 +02:00
parent 669bc071b1
commit 190d7cefb0
2 changed files with 31 additions and 1 deletions

View File

@@ -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;

View File

@@ -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<AutogenerateableId, String> {