DATADOC-246 - Added DBRef to Mongo simple types.
General overhaul of default setup of custom Mongo simple types. MongoTemplate.doUpdate(…) transparently handles null queries now as well.
This commit is contained in:
@@ -679,7 +679,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
|
||||
MongoPersistentEntity<?> entity = entityClass == null ? null : getPersistentEntity(entityClass);
|
||||
|
||||
DBObject queryObj = mapper.getMappedObject(query.getQueryObject(), entity);
|
||||
DBObject queryObj = query == null ? new BasicDBObject() : mapper.getMappedObject(query.getQueryObject(), entity);
|
||||
DBObject updateObj = update.getUpdateObject();
|
||||
|
||||
for (String key : updateObj.keySet()) {
|
||||
@@ -1123,7 +1123,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
|
||||
}
|
||||
|
||||
private static final MongoConverter getDefaultMongoConverter(MongoDbFactory factory) {
|
||||
// ToDo: maybe add some additional configurations to this very basic one
|
||||
MappingMongoConverter converter = new MappingMongoConverter(factory, new MongoMappingContext());
|
||||
converter.afterPropertiesSet();
|
||||
return converter;
|
||||
|
||||
@@ -34,6 +34,7 @@ import org.springframework.core.convert.support.GenericConversionService;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.BigDecimalToStringConverter;
|
||||
import org.springframework.data.mongodb.core.convert.MongoConverters.StringToBigDecimalConverter;
|
||||
import org.springframework.data.mongodb.core.mapping.MongoSimpleTypes;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.mongodb.DBObject;
|
||||
@@ -48,7 +49,7 @@ import com.mongodb.DBObject;
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class CustomConversions {
|
||||
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
private static final List<Class<?>> MONGO_TYPES = Arrays.asList(Number.class, Date.class, ObjectId.class, String.class,
|
||||
DBObject.class);
|
||||
@@ -79,7 +80,6 @@ public class CustomConversions {
|
||||
this.readingPairs = new HashSet<ConvertiblePair>();
|
||||
this.writingPairs = new HashSet<ConvertiblePair>();
|
||||
this.customSimpleTypes = new HashSet<Class<?>>();
|
||||
this.customSimpleTypes.add(ObjectId.class);
|
||||
|
||||
this.converters = new ArrayList<Object>();
|
||||
this.converters.add(CustomToStringConverter.INSTANCE);
|
||||
@@ -91,7 +91,7 @@ public class CustomConversions {
|
||||
registerConversion(c);
|
||||
}
|
||||
|
||||
this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes, true);
|
||||
this.simpleTypeHolder = new SimpleTypeHolder(customSimpleTypes, MongoSimpleTypes.HOLDER);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,11 +18,7 @@ package org.springframework.data.mongodb.core.mapping;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bson.types.CodeWScope;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.mapping.context.AbstractMappingContext;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
@@ -32,23 +28,12 @@ import org.springframework.data.util.TypeInformation;
|
||||
* @author Oliver Gierke ogierke@vmware.com
|
||||
*/
|
||||
public class MongoMappingContext extends AbstractMappingContext<BasicMongoPersistentEntity<?>, MongoPersistentProperty> {
|
||||
|
||||
private static final Set<Class<?>> MONGO_SIMPLE_TYPES = new HashSet<Class<?>>();
|
||||
|
||||
static {
|
||||
MONGO_SIMPLE_TYPES.add(com.mongodb.DBRef.class);
|
||||
MONGO_SIMPLE_TYPES.add(ObjectId.class);
|
||||
MONGO_SIMPLE_TYPES.add(CodeWScope.class);
|
||||
MONGO_SIMPLE_TYPES.add(Character.class);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.mapping.AbstractMappingContext#setSimpleTypeHolder(org.springframework.data.mapping.SimpleTypeHolder)
|
||||
|
||||
/**
|
||||
* Creates a new {@link MongoMappingContext}.
|
||||
*/
|
||||
@Override
|
||||
public void setSimpleTypeHolder(SimpleTypeHolder simpleTypes) {
|
||||
super.setSimpleTypeHolder(new SimpleTypeHolder(MONGO_SIMPLE_TYPES, simpleTypes));
|
||||
public MongoMappingContext() {
|
||||
setSimpleTypeHolder(MongoSimpleTypes.HOLDER);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright (c) 2011 by the original author(s).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.mongodb.core.mapping;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bson.types.CodeWScope;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.mapping.model.SimpleTypeHolder;
|
||||
|
||||
import com.mongodb.DBRef;
|
||||
|
||||
/**
|
||||
* Simple constant holder for a {@link SimpleTypeHolder} enriched with Mongo specific simple types.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class MongoSimpleTypes {
|
||||
|
||||
private static final Set<Class<?>> MONGO_SIMPLE_TYPES = new HashSet<Class<?>>();
|
||||
|
||||
static {
|
||||
MONGO_SIMPLE_TYPES.add(DBRef.class);
|
||||
MONGO_SIMPLE_TYPES.add(ObjectId.class);
|
||||
MONGO_SIMPLE_TYPES.add(CodeWScope.class);
|
||||
}
|
||||
|
||||
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true);
|
||||
|
||||
private MongoSimpleTypes() {
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBObject;
|
||||
import com.mongodb.DBRef;
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoException;
|
||||
import com.mongodb.WriteResult;
|
||||
@@ -772,4 +773,20 @@ public class MongoTemplateTests {
|
||||
assertThat(result.getId(), is(person.getId()));
|
||||
assertThat(result.getFirstName(), is("Carter"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATADOC-246
|
||||
*/
|
||||
@Test
|
||||
public void updatesDBRefsCorrectly() {
|
||||
|
||||
DBRef first = new DBRef(factory.getDb(), "foo", new ObjectId());
|
||||
DBRef second = new DBRef(factory.getDb(), "bar", new ObjectId());
|
||||
|
||||
template.updateFirst(null, Update.update("dbRefs", Arrays.asList(first, second)), ClassWithDBRefs.class);
|
||||
}
|
||||
|
||||
class ClassWithDBRefs {
|
||||
List<DBRef> dbrefs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.core.convert.support.GenericConversionService;
|
||||
|
||||
import com.mongodb.DBRef;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CustomConversions}.
|
||||
*
|
||||
@@ -82,6 +84,13 @@ public class CustomConversionsUnitTests {
|
||||
assertThat(conversions.hasCustomReadTarget(ObjectId.class, String.class), is(true));
|
||||
assertThat(conversions.hasCustomReadTarget(ObjectId.class, Object.class), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersDBRefsToBeSimpleTypes() {
|
||||
|
||||
CustomConversions conversions = new CustomConversions();
|
||||
assertThat(conversions.isSimpleType(DBRef.class), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void populatesConversionServiceCorrectly() {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
|
||||
<property name="host" value="localhost"/>
|
||||
<property name="host" value="127.0.0.1"/>
|
||||
<property name="port" value="27017"/>
|
||||
</bean>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user