DATADOC-95 - Fix issue where an object will all null properties wasn't being saved.

This commit is contained in:
Jon Brisbin
2011-04-27 07:52:01 -05:00
committed by J. Brisbin
parent dc36d91d8e
commit 7567ba0355
3 changed files with 53 additions and 4 deletions

View File

@@ -701,9 +701,10 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica
protected Object insertDBObject(String collectionName, final DBObject dbDoc) {
if (dbDoc.keySet().isEmpty()) {
return null;
}
// DATADOC-95: This will prevent null objects from being saved.
//if (dbDoc.keySet().isEmpty()) {
//return null;
//}
//TODO: Need to move this to more central place
if (dbDoc.containsField("_id")) {

View File

@@ -57,6 +57,7 @@ public class MappingTests {
"personmultidimarrays",
"personmulticollection",
"personwithdbref",
"personnullproperties",
"person1",
"person2",
"account"
@@ -302,7 +303,8 @@ public class MappingTests {
@Test
public void testDbRef() {
GeoLocation geo = new GeoLocation(new double[]{37.0625, -95.677068});
double[] pos = new double[]{37.0625, -95.677068};
GeoLocation geo = new GeoLocation(pos);
template.insert(geo);
PersonWithDbRef p = new PersonWithDbRef(4321, "With", "DBRef", geo);
@@ -310,6 +312,15 @@ public class MappingTests {
List<PersonWithDbRef> result = template.find(new Query(Criteria.where("ssn").is(4321)), PersonWithDbRef.class);
assertThat(result.size(), is(1));
assertThat(result.get(0).getHome().getLocation(), is(pos));
}
@Test
public void testPersonWithNullProperties() {
PersonNullProperties p = new PersonNullProperties();
template.insert(p);
assertNotNull(p.getId());
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.document.mongodb.mapping;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
@Document
public class PersonNullProperties extends BasePerson {
@Id
private ObjectId id;
public PersonNullProperties() {
}
public ObjectId getId() {
return id;
}
}