DATADOC-275 - Fixed id handling for DBRefs.

Trying to convert target ids into ObjectId or String before using the actual type.
This commit is contained in:
Oliver Gierke
2011-09-30 11:50:44 +02:00
parent 7d26366352
commit 9f8e406aff
2 changed files with 61 additions and 12 deletions

View File

@@ -50,6 +50,7 @@ import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.SpELAwareParameterValueProvider;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.QueryMapper;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.util.ClassTypeInformation;
@@ -87,6 +88,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
protected final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
protected final SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
protected final MongoDbFactory mongoDbFactory;
protected final QueryMapper idMapper;
protected ApplicationContext applicationContext;
protected boolean useFieldAccessOnly = true;
protected MongoTypeMapper typeMapper;
@@ -109,6 +111,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
this.mongoDbFactory = mongoDbFactory;
this.mappingContext = mappingContext;
this.typeMapper = new DefaultMongoTypeMapper(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY, mappingContext);
this.idMapper = new QueryMapper(conversionService);
}
/**
@@ -672,6 +675,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
}
protected DBRef createDBRef(Object target, org.springframework.data.mongodb.core.mapping.DBRef dbref) {
MongoPersistentEntity<?> targetEntity = mappingContext.getPersistentEntity(target.getClass());
if (null == targetEntity || null == targetEntity.getIdProperty()) {
return null;
@@ -680,6 +684,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
MongoPersistentProperty idProperty = targetEntity.getIdProperty();
Object id = null;
BeanWrapper<MongoPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(target, conversionService);
try {
id = wrapper.getProperty(idProperty, Object.class, useFieldAccessOnly);
if (null == id) {
@@ -698,7 +703,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
String dbname = dbref.db();
DB db = StringUtils.hasText(dbname) ? mongoDbFactory.getDb(dbname) : mongoDbFactory.getDb();
return new DBRef(db, collection, id);
return new DBRef(db, collection, idMapper.convertId(id));
}
@SuppressWarnings("unchecked")

View File

@@ -23,6 +23,7 @@ import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.core.query.Update.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -34,11 +35,13 @@ import com.mongodb.Mongo;
import com.mongodb.MongoException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.MongoCollectionUtils;
import org.springframework.data.mongodb.core.CollectionCallback;
import org.springframework.data.mongodb.core.MongoDbUtils;
@@ -459,16 +462,56 @@ public class MappingTests {
assertNotNull(p2.getPersonPojoLongId());
assertEquals(12L, p2.getPersonPojoLongId().getId());
}
/**
* @see DATADOC-275
*/
@Test
public void readsAndWritesDBRefsCorrectly() {
// @Test
// public void testThroughput() {
// long start = System.currentTimeMillis();
// for (int i = 0; i < 10000; i++) {
// PersonPojo p = new PersonPojo(i, "throughput test", "");
// template.insert(p);
// }
// double elapsed = System.currentTimeMillis() - start;
// System.out.println("time: " + (elapsed / 1000) + "s");
// }
template.dropCollection(Item.class);
template.dropCollection(Container.class);
Item item = new Item();
Item items = new Item();
template.insert(item);
template.insert(items);
Container container = new Container();
container.item = item;
container.items = Arrays.asList(items);
template.insert(container);
Container result = template.findOne(query(where("id").is(container.id)), Container.class);
assertThat(result.item.id, is(item.id));
assertThat(result.items.size(), is(1));
assertThat(result.items.get(0).id, is(items.id));
}
class Container {
@Id
final String id;
public Container() {
id = new ObjectId().toString();
}
@DBRef
Item item;
@DBRef
List<Item> items;
}
class Item {
@Id
final String id;
public Item() {
this.id = new ObjectId().toString();
}
}
}