DATACMNS-562 - AbstractPersistentProperty doesn't consider maps and collections entities anymore.

The type detection for entity candidates of PersistentProperty instances now consistently handles collection types even if they're used als collection or map values.
This commit is contained in:
Oliver Gierke
2014-08-19 16:38:15 +02:00
parent 33698a7649
commit 6daf7e2752
2 changed files with 31 additions and 19 deletions

View File

@@ -18,8 +18,7 @@ package org.springframework.data.mapping.model;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import org.springframework.core.annotation.AnnotationUtils;
@@ -118,23 +117,23 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
*/
public Iterable<? extends TypeInformation<?>> getPersistentEntityType() {
List<TypeInformation<?>> result = new ArrayList<TypeInformation<?>>();
TypeInformation<?> type = getTypeInformation();
if (type.isCollectionLike() || isMap()) {
TypeInformation<?> nestedType = getTypeInformationIfNotSimpleType(getTypeInformation().getActualType());
if (nestedType != null) {
result.add(nestedType);
}
} else if (isEntity()) {
result.add(type);
if (!isEntity()) {
return Collections.emptySet();
}
return result;
TypeInformation<?> candidate = getTypeInformationIfEntityCandidate();
return candidate != null ? Collections.singleton(candidate) : Collections.<TypeInformation<?>> emptySet();
}
private TypeInformation<?> getTypeInformationIfNotSimpleType(TypeInformation<?> information) {
return information == null || simpleTypeHolder.isSimpleType(information.getType()) ? null : information;
private TypeInformation<?> getTypeInformationIfEntityCandidate() {
TypeInformation<?> candidate = information.getActualType();
if (candidate == null || simpleTypeHolder.isSimpleType(candidate.getType())) {
return null;
}
return candidate.isCollectionLike() || candidate.isMap() ? null : candidate;
}
/*
@@ -248,10 +247,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
* @see org.springframework.data.mapping.PersistentProperty#isEntity()
*/
public boolean isEntity() {
TypeInformation<?> actualType = information.getActualType();
boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType());
return isComplexType && !isTransient();
return !isTransient() && getTypeInformationIfEntityCandidate() != null;
}
/*

View File

@@ -26,6 +26,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
import org.junit.Before;
@@ -260,6 +261,17 @@ public class AbstractPersistentPropertyUnitTests {
assertThat(property.isEntity(), is(false));
}
/**
* @see DATACMNS-562
*/
@Test
public void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() {
SamplePersistentProperty property = getProperty(TreeMapWrapper.class, "map");
assertThat(property.getPersistentEntityType(), is(emptyIterable()));
assertThat(property.isEntity(), is(false));
}
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>(
@@ -401,4 +413,8 @@ public class AbstractPersistentPropertyUnitTests {
Map<String, Person> personMap;
Collection<String> strings;
}
class TreeMapWrapper {
TreeMap<String, TreeMap<String, String>> map;
}
}