From 47b8523cb39abddbfbd578e69985be4faa5f6010 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 12 Mar 2011 11:55:17 +0100 Subject: [PATCH] Removed Mongo specific id handling from ConfigurationBuilder. Introduced MongoPersistentProperty that implements handling of fields with certain types and names as id property. --- .../MongoMappingConfigurationBuilder.java | 16 +--- .../mapping/MongoPersistentProperty.java | 76 +++++++++++++++++++ 2 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoPersistentProperty.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingConfigurationBuilder.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingConfigurationBuilder.java index b8c2f4275..33ad9ddd4 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingConfigurationBuilder.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingConfigurationBuilder.java @@ -61,7 +61,8 @@ public class MongoMappingConfigurationBuilder extends BasicMappingConfigurationB @Override public PersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor) throws MappingConfigurationException { - PersistentProperty property = super.createPersistentProperty(field, descriptor); + @SuppressWarnings({ "unchecked", "rawtypes" }) + PersistentProperty property = new MongoPersistentProperty(field.getName(), field.getType(), field, descriptor); if (field.isAnnotationPresent(Indexed.class)) { Indexed index = field.getAnnotation(Indexed.class); String collection = index.collection(); @@ -122,19 +123,6 @@ public class MongoMappingConfigurationBuilder extends BasicMappingConfigurationB return super.createAssociation(property); } - @Override - protected boolean isIdField(Field field) { - if (super.isIdField(field)) { - return true; - } - if (field.getType() == ObjectId.class || field.getType() == BigInteger.class) { - if ("id".equals(field.getName()) || "_id".equals(field.getName())) { - return true; - } - } - return false; - } - protected void ensureIndex(String collection, final String name, final String def, diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoPersistentProperty.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoPersistentProperty.java new file mode 100644 index 000000000..8540820ac --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoPersistentProperty.java @@ -0,0 +1,76 @@ +/* + * Copyright 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 java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.math.BigInteger; +import java.util.HashSet; +import java.util.Set; + +import org.bson.types.ObjectId; +import org.springframework.data.mapping.BasicPersistentProperty; + +/** + * Mongo specific + * {@link org.springframework.data.mapping.model.PersistentProperty} + * implementation. + * + * @author Oliver Gierke + */ +public class MongoPersistentProperty extends BasicPersistentProperty { + + private static final Set> SUPPORTED_ID_TYPES = new HashSet>(); + private static final Set SUPPORTED_ID_PROPERTY_NAMES = new HashSet(); + + static { + SUPPORTED_ID_TYPES.add(ObjectId.class); + SUPPORTED_ID_TYPES.add(String.class); + SUPPORTED_ID_TYPES.add(BigInteger.class); + + SUPPORTED_ID_PROPERTY_NAMES.add("id"); + SUPPORTED_ID_PROPERTY_NAMES.add("_id"); + } + + /** + * Creates a new {@link MongoPersistentProperty}. + * + * @param name + * @param type + * @param field + * @param propertyDescriptor + */ + public MongoPersistentProperty(String name, Class type, Field field, + PropertyDescriptor propertyDescriptor) { + super(name, type, field, propertyDescriptor); + } + + /** + * Also considers fields as id that are of supported id type and name. + * + * @see #SUPPORTED_ID_PROPERTY_NAMES + * @see #SUPPORTED_ID_TYPES + */ + @Override + public boolean isIdProperty() { + if (super.isIdProperty()) { + return true; + } + + return SUPPORTED_ID_TYPES.contains(field.getType()) + && SUPPORTED_ID_PROPERTY_NAMES.contains(field.getName()); + } +}