DATADOC-155 - Add support for any type for id properties, not just the ones that can be converted into an ObjectId

This commit is contained in:
J. Brisbin
2011-05-27 14:22:58 -05:00
parent 7ea14bb4d5
commit 6a73e94c57
3 changed files with 73 additions and 19 deletions

View File

@@ -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<MongoPersistentProperty> implements
MongoPersistentProperty {
private static final Log LOG = LogFactory.getLog(BasicMongoPersistentProperty.class);
private static final Set<Class<?>> SUPPORTED_ID_TYPES = new HashSet<Class<?>>();
@@ -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<MongoPersistentProperty> createAssociation() {
return new Association<MongoPersistentProperty>(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);
}

View File

@@ -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());
}
}

View File

@@ -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 <jbrisbin@vmware.com>
*/
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;
}
}