From 6a73e94c57f73dc39f3d7e2b62f57808678043f1 Mon Sep 17 00:00:00 2001 From: "J. Brisbin" Date: Fri, 27 May 2011 14:22:58 -0500 Subject: [PATCH] DATADOC-155 - Add support for any type for id properties, not just the ones that can be converted into an ObjectId --- .../mapping/BasicMongoPersistentProperty.java | 37 ++++++++-------- .../mongodb/mapping/MappingTests.java | 12 +++++- .../mongodb/mapping/PersonPojoIntId.java | 43 +++++++++++++++++++ 3 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/BasicMongoPersistentProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/BasicMongoPersistentProperty.java index cba86b164..b9c1ba8ea 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/BasicMongoPersistentProperty.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/BasicMongoPersistentProperty.java @@ -21,22 +21,21 @@ import java.math.BigInteger; import java.util.HashSet; import java.util.Set; +import com.mongodb.DBObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.bson.types.ObjectId; import org.springframework.data.mapping.AnnotationBasedPersistentProperty; import org.springframework.data.mapping.model.Association; -import com.mongodb.DBObject; - /** * Mongo specific {@link org.springframework.data.mapping.model.PersistentProperty} implementation. - * + * * @author Oliver Gierke */ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentProperty implements MongoPersistentProperty { - + private static final Log LOG = LogFactory.getLog(BasicMongoPersistentProperty.class); private static final Set> SUPPORTED_ID_TYPES = new HashSet>(); @@ -53,14 +52,14 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope /** * Creates a new {@link BasicMongoPersistentProperty}. - * + * * @param field * @param propertyDescriptor - * @param owningTypeInformation + * @param owner */ public BasicMongoPersistentProperty(Field field, PropertyDescriptor propertyDescriptor, MongoPersistentEntity owner) { super(field, propertyDescriptor, owner); - + if (isIdProperty() && field.isAnnotationPresent(FieldName.class)) { LOG.warn(String.format("Invalid usage of %s on id property. Field name will not be considered!", FieldName.class)); } @@ -76,7 +75,7 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope /** * Also considers fields as id that are of supported id type and name. - * + * * @see #SUPPORTED_ID_PROPERTY_NAMES * @see #SUPPORTED_ID_TYPES */ @@ -86,20 +85,22 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope return true; } - return SUPPORTED_ID_TYPES.contains(field.getType()) && SUPPORTED_ID_PROPERTY_NAMES.contains(field.getName()); + // We need to support a wider range of ID types than just the ones that can be converted to an ObjectId + return SUPPORTED_ID_PROPERTY_NAMES.contains(field.getName()); + //return SUPPORTED_ID_TYPES.contains(field.getType()) && SUPPORTED_ID_PROPERTY_NAMES.contains(field.getName()); } /** * Returns the key to be used to store the value of the property inside a Mongo {@link DBObject}. - * + * * @return */ public String getFieldName() { - + if (isIdProperty()) { return "_id"; } - + FieldName annotation = getField().getAnnotation(FieldName.class); return annotation != null ? annotation.value() : getName(); } @@ -111,17 +112,17 @@ public class BasicMongoPersistentProperty extends AnnotationBasedPersistentPrope protected Association createAssociation() { return new Association(this, null); } - + /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.mapping.MongoPersistentProperty#isDbReference() - */ + * @see org.springframework.data.document.mongodb.mapping.MongoPersistentProperty#isDbReference() + */ public boolean isDbReference() { return getField().isAnnotationPresent(DBRef.class); } - + /* (non-Javadoc) - * @see org.springframework.data.document.mongodb.mapping.MongoPersistentProperty#getDBRef() - */ + * @see org.springframework.data.document.mongodb.mapping.MongoPersistentProperty#getDBRef() + */ public DBRef getDBRef() { return getField().getAnnotation(DBRef.class); } 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 092a22d21..f00997729 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 @@ -66,12 +66,13 @@ public class MappingTests { "foobar", "geolocation", "person1", "person2", "account"}; ApplicationContext applicationContext; + Mongo mongo; MongoTemplate template; MongoMappingContext mappingContext; @Before public void setUp() throws Exception { - Mongo mongo = new Mongo(); + mongo = new Mongo(); DB db = mongo.getDB("database"); for (String coll : collectionsToDrop) { db.getCollection(coll).drop(); @@ -367,7 +368,16 @@ public class MappingTests { PrimitiveId p2 = template.findOne(query(where("id").is(1)), PrimitiveId.class); assertNotNull(p2); + } + @Test + public void testNoMappingAnnotations() { + PersonPojoIntId p = new PersonPojoIntId(1, "Text"); + template.save(p); + template.updateFirst(PersonPojoIntId.class, query(where("id").is(1)), update("text", "New Text")); + + PersonPojoIntId p2 = template.findOne(query(where("id").is(1)), PersonPojoIntId.class); + assertEquals("New Text", p2.getText()); } } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java new file mode 100644 index 000000000..a59d9525b --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/PersonPojoIntId.java @@ -0,0 +1,43 @@ +/* + * 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; + +/** + * @author Jon Brisbin + */ +public class PersonPojoIntId { + + private Integer id; + private String text; + + public PersonPojoIntId(Integer id, String text) { + this.id = id; + this.text = text; + } + + public Integer getId() { + return id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +}