DATACMNS-479 - Make sure Map types are not added to the mapping context.

Changed the algorithm for nested PersistentEntity detection to only use PersistentProperty.getPersistentEntityTypes(…) and make sure we return unwrap Maps and Collections inside the method correctly.
This commit is contained in:
Oliver Gierke
2014-03-27 11:56:17 +01:00
parent cb40c4e660
commit df16f9535c
3 changed files with 26 additions and 13 deletions

View File

@@ -402,10 +402,6 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
return;
}
if (!property.isEntity()) {
return;
}
for (TypeInformation<?> candidate : property.getPersistentEntityType()) {
addPersistentEntity(candidate);
}

View File

@@ -113,18 +113,15 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
public Iterable<? extends TypeInformation<?>> getPersistentEntityType() {
List<TypeInformation<?>> result = new ArrayList<TypeInformation<?>>();
TypeInformation<?> type = getTypeInformation();
if (isEntity()) {
result.add(type);
}
if (type.isCollectionLike() || isMap()) {
TypeInformation<?> nestedType = getTypeInformationIfNotSimpleType(getTypeInformation().getActualType());
if (nestedType != null) {
result.add(nestedType);
}
} else if (isEntity()) {
result.add(type);
}
return result;

View File

@@ -20,10 +20,10 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import groovy.lang.MetaClass;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import org.junit.Before;
import org.junit.Test;
@@ -232,14 +232,33 @@ public class AbstractMappingContextUnitTests {
public void hasPersistentEntityForCollectionPropertiesAfterInitialization() {
context.getPersistentEntity(Sample.class);
assertHasEntityFor(Person.class, context, true);
}
/**
* @see DATACMNS-479
*/
@Test
public void doesNotAddMapImplementationClassesAsPersistentEntity() {
context.getPersistentEntity(Sample.class);
assertHasEntityFor(TreeMap.class, context, false);
}
private static void assertHasEntityFor(Class<?> type, SampleMappingContext context, boolean expected) {
boolean found = false;
for (BasicPersistentEntity<Object, SamplePersistentProperty> entity : context.getPersistentEntities()) {
if (entity.getType().equals(Person.class)) {
return;
if (entity.getType().equals(type)) {
found = true;
break;
}
}
fail("Expected to find persistent entity for Person!");
if (found != expected) {
fail(String.format("%s to find persistent entity for %s!", expected ? "Expected" : "Did not expect", type));
}
}
class Person {
@@ -254,6 +273,7 @@ public class AbstractMappingContextUnitTests {
MetaClass metaClass;
List<Person> persons;
TreeMap<String, Person> personMap;
}
static class Base {