From c9dd679cd1706ca5ef7065fb17e5b451034bed7c Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 19 Aug 2014 16:38:15 +0200 Subject: [PATCH] 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. --- .../model/AbstractPersistentProperty.java | 34 ++++++++----------- .../AbstractPersistentPropertyUnitTests.java | 16 +++++++++ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index b39569c7f..1093c5172 100644 --- a/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -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; @@ -125,23 +124,23 @@ public abstract class AbstractPersistentProperty

@Override public Iterable> getPersistentEntityType() { - List> result = new ArrayList>(); - 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.> 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; } /* @@ -271,10 +270,7 @@ public abstract class AbstractPersistentProperty

*/ @Override public boolean isEntity() { - - TypeInformation actualType = information.getActualType(); - boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType()); - return isComplexType && !isTransient(); + return !isTransient() && getTypeInformationIfEntityCandidate() != null; } /* diff --git a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index de273bd2a..df63d816d 100644 --- a/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -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 SamplePersistentProperty getProperty(Class type, String name) { BasicPersistentEntity entity = new BasicPersistentEntity( @@ -401,4 +413,8 @@ public class AbstractPersistentPropertyUnitTests { Map personMap; Collection strings; } + + class TreeMapWrapper { + TreeMap> map; + } }