diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java index 039b93dea..0cc3c9569 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicMappingContext.java @@ -26,6 +26,8 @@ import org.springframework.core.convert.support.ConversionServiceFactory; import org.springframework.core.convert.support.GenericConversionService; import org.springframework.data.mapping.model.*; import org.springframework.util.Assert; +import org.springframework.util.ReflectionUtils; +import org.springframework.util.ReflectionUtils.FieldCallback; import org.springframework.validation.Validator; import java.beans.BeanInfo; @@ -83,32 +85,36 @@ public class BasicMappingContext implements MappingContext, InitializingBean { public PersistentEntity addPersistentEntity(Class type) { if (null == persistentEntities.get(type.getName())) { try { - PersistentEntity entity = builder.createPersistentEntity(type, this); + final PersistentEntity entity = builder.createPersistentEntity(type, this); BeanInfo info = Introspector.getBeanInfo(type); - Map descriptors = new HashMap(); + final Map descriptors = new HashMap(); for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { descriptors.put(descriptor.getName(), descriptor); } - - List fields = new LinkedList(Arrays.asList(type.getDeclaredFields())); - Class superClazz = type.getSuperclass(); - while (Object.class != superClazz) { - fields.addAll(new LinkedList(Arrays.asList(superClazz.getDeclaredFields()))); - superClazz = superClazz.getSuperclass(); - } - for (Field field : fields) { - PropertyDescriptor descriptor = descriptors.get(field.getName()); - if (builder.isPersistentProperty(field, descriptor)) { - PersistentProperty property = builder.createPersistentProperty(field, descriptor); - property.setOwner(entity); - entity.addPersistentProperty(property); - if (builder.isAssociation(field, descriptor)) { - Association association = builder.createAssociation(property); - entity.addAssociation(association); + + ReflectionUtils.doWithFields(type, new FieldCallback() { + + @Override + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { + + try { + PropertyDescriptor descriptor = descriptors.get(field.getName()); + if (builder.isPersistentProperty(field, descriptor)) { + PersistentProperty property = builder.createPersistentProperty(field, descriptor); + property.setOwner(entity); + entity.addPersistentProperty(property); + if (builder.isAssociation(field, descriptor)) { + Association association = builder.createAssociation(property); + entity.addAssociation(association); + } + } + } catch (MappingConfigurationException e) { + log.error(e.getMessage(), e); } } - } + }); + entity.setIdProperty(builder.getIdProperty(type)); entity.setPreferredConstructor(builder.getPreferredConstructor(type));