Browse Source

Removed Mongo specific id handling from ConfigurationBuilder.

Introduced MongoPersistentProperty that implements handling of fields with certain types and names as id property.
pull/1/head
Oliver Gierke 15 years ago
parent
commit
47b8523cb3
  1. 16
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingConfigurationBuilder.java
  2. 76
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoPersistentProperty.java

16
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoMappingConfigurationBuilder.java

@ -61,7 +61,8 @@ public class MongoMappingConfigurationBuilder extends BasicMappingConfigurationB @@ -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 @@ -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,

76
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/mapping/MongoPersistentProperty.java

@ -0,0 +1,76 @@ @@ -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<T> extends BasicPersistentProperty<T> {
private static final Set<Class<?>> SUPPORTED_ID_TYPES = new HashSet<Class<?>>();
private static final Set<String> SUPPORTED_ID_PROPERTY_NAMES = new HashSet<String>();
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<T> 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());
}
}
Loading…
Cancel
Save