Skip to content

Commit c9dd679

Browse files
committed
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.
1 parent 77a6702 commit c9dd679

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import java.beans.PropertyDescriptor;
1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Method;
21-
import java.util.ArrayList;
22-
import java.util.List;
21+
import java.util.Collections;
2322
import java.util.Map;
2423

2524
import org.springframework.core.annotation.AnnotationUtils;
@@ -125,23 +124,23 @@ public TypeInformation<?> getTypeInformation() {
125124
@Override
126125
public Iterable<? extends TypeInformation<?>> getPersistentEntityType() {
127126

128-
List<TypeInformation<?>> result = new ArrayList<TypeInformation<?>>();
129-
TypeInformation<?> type = getTypeInformation();
130-
131-
if (type.isCollectionLike() || isMap()) {
132-
TypeInformation<?> nestedType = getTypeInformationIfNotSimpleType(getTypeInformation().getActualType());
133-
if (nestedType != null) {
134-
result.add(nestedType);
135-
}
136-
} else if (isEntity()) {
137-
result.add(type);
127+
if (!isEntity()) {
128+
return Collections.emptySet();
138129
}
139130

140-
return result;
131+
TypeInformation<?> candidate = getTypeInformationIfEntityCandidate();
132+
return candidate != null ? Collections.singleton(candidate) : Collections.<TypeInformation<?>> emptySet();
141133
}
142134

143-
private TypeInformation<?> getTypeInformationIfNotSimpleType(TypeInformation<?> information) {
144-
return information == null || simpleTypeHolder.isSimpleType(information.getType()) ? null : information;
135+
private TypeInformation<?> getTypeInformationIfEntityCandidate() {
136+
137+
TypeInformation<?> candidate = information.getActualType();
138+
139+
if (candidate == null || simpleTypeHolder.isSimpleType(candidate.getType())) {
140+
return null;
141+
}
142+
143+
return candidate.isCollectionLike() || candidate.isMap() ? null : candidate;
145144
}
146145

147146
/*
@@ -271,10 +270,7 @@ public boolean isArray() {
271270
*/
272271
@Override
273272
public boolean isEntity() {
274-
275-
TypeInformation<?> actualType = information.getActualType();
276-
boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType());
277-
return isComplexType && !isTransient();
273+
return !isTransient() && getTypeInformationIfEntityCandidate() != null;
278274
}
279275

280276
/*

src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.lang.reflect.Field;
2727
import java.util.Collection;
2828
import java.util.Map;
29+
import java.util.TreeMap;
2930
import java.util.TreeSet;
3031

3132
import org.junit.Before;
@@ -260,6 +261,17 @@ public void considersCollectionPropertySimpleIfComponentTypeIsSimple() {
260261
assertThat(property.isEntity(), is(false));
261262
}
262263

264+
/**
265+
* @see DATACMNS-562
266+
*/
267+
@Test
268+
public void doesNotConsiderPropertyWithTreeMapMapValueAnEntity() {
269+
270+
SamplePersistentProperty property = getProperty(TreeMapWrapper.class, "map");
271+
assertThat(property.getPersistentEntityType(), is(emptyIterable()));
272+
assertThat(property.isEntity(), is(false));
273+
}
274+
263275
private <T> SamplePersistentProperty getProperty(Class<T> type, String name) {
264276

265277
BasicPersistentEntity<T, SamplePersistentProperty> entity = new BasicPersistentEntity<T, SamplePersistentProperty>(
@@ -401,4 +413,8 @@ static class Sample {
401413
Map<String, Person> personMap;
402414
Collection<String> strings;
403415
}
416+
417+
class TreeMapWrapper {
418+
TreeMap<String, TreeMap<String, String>> map;
419+
}
404420
}

0 commit comments

Comments
 (0)