From 7567ba0355a204f90a0383f124ce5f2f4d79464f Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Wed, 27 Apr 2011 07:52:01 -0500 Subject: [PATCH] DATADOC-95 - Fix issue where an object will all null properties wasn't being saved. --- .../data/document/mongodb/MongoTemplate.java | 7 ++-- .../mongodb/mapping/MappingTests.java | 13 ++++++- .../mongodb/mapping/PersonNullProperties.java | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonNullProperties.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java index e6b381f21..a43584be5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java @@ -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")) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java index 8af8e637c..cbcdb1743 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java @@ -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 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()); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonNullProperties.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonNullProperties.java new file mode 100644 index 000000000..bdab2a319 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonNullProperties.java @@ -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 + */ +@Document +public class PersonNullProperties extends BasePerson { + + @Id + private ObjectId id; + + public PersonNullProperties() { + } + + public ObjectId getId() { + return id; + } +}