Only consider properties that are backed by a field actually.

Changed MongoPropertyDescriptor.isMappable(…) to check whether the owning type actually has a field with the property name. If not the property will not be regarded as mappable. This results in plain functional getters (that are not a bean property getter actually) to be skipped transparently.
This commit is contained in:
Oliver Gierke
2011-03-16 11:47:06 +01:00
parent 4b7d96347c
commit f747f3df60
2 changed files with 13 additions and 4 deletions

View File

@@ -24,6 +24,7 @@ import java.util.*;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* An iterable of {@link MongoPropertyDescriptor}s that allows dedicated access to the {@link MongoPropertyDescriptor}
@@ -48,7 +49,7 @@ public class MongoPropertyDescriptors implements Iterable<MongoPropertyDescripto
MongoPropertyDescriptors.MongoPropertyDescriptor idDesciptor = null;
for (PropertyDescriptor candidates : BeanUtils.getPropertyDescriptors(type)) {
MongoPropertyDescriptor descriptor = new MongoPropertyDescriptors.MongoPropertyDescriptor(candidates);
MongoPropertyDescriptor descriptor = new MongoPropertyDescriptors.MongoPropertyDescriptor(candidates, type);
descriptors.add(descriptor);
if (descriptor.isIdProperty()) {
idDesciptor = descriptor;
@@ -98,15 +99,18 @@ public class MongoPropertyDescriptors implements Iterable<MongoPropertyDescripto
static final String ID_KEY = "_id";
private final PropertyDescriptor delegate;
private final Class<?> owningType;
/**
* Creates a new {@link MongoPropertyDescriptor} for the given {@link PropertyDescriptor}.
*
* @param descriptor
* @param owningType
*/
public MongoPropertyDescriptor(PropertyDescriptor descriptor) {
public MongoPropertyDescriptor(PropertyDescriptor descriptor, Class<?> owningType) {
Assert.notNull(descriptor);
this.delegate = descriptor;
this.owningType = owningType;
}
/**
@@ -154,7 +158,12 @@ public class MongoPropertyDescriptors implements Iterable<MongoPropertyDescripto
* @return
*/
public boolean isMappable() {
return !delegate.getName().equals("class") && delegate.getReadMethod() != null;
boolean isNotClassAttribute = !delegate.getName().equals("class");
boolean hasGetter = delegate.getReadMethod() != null;
boolean hasField = ReflectionUtils.findField(owningType, delegate.getName()) != null;
return isNotClassAttribute && hasGetter && hasField;
}
/**

View File

@@ -942,7 +942,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
}
final MongoPropertyDescriptor descriptor;
try {
descriptor = new MongoPropertyDescriptor(new PropertyDescriptor(idKey, targetClass));
descriptor = new MongoPropertyDescriptor(new PropertyDescriptor(idKey, targetClass), targetClass);
} catch (IntrospectionException e) {
// no property descriptor for this key
return;