diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 105f7365a..42979ead2 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -49,6 +49,7 @@ import org.springframework.data.mapping.model.MutablePersistentEntity; import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.TypeInformation; +import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.FieldCallback; @@ -155,6 +156,7 @@ public abstract class AbstractMappingContext type) { + Assert.notNull(type); return getPersistentEntity(ClassTypeInformation.from(type)); } @@ -164,6 +166,8 @@ public abstract class AbstractMappingContext type) { + Assert.notNull(type); + try { read.lock(); E entity = persistentEntities.get(type); @@ -180,6 +184,10 @@ public abstract class AbstractMappingContext type) { - return addPersistentEntity(ClassTypeInformation.from(type)); } @@ -345,6 +352,19 @@ public abstract class AbstractMappingContext type) { + return !simpleTypeHolder.isSimpleType(type.getType()); + } + /** * {@link FieldCallback} to create {@link PersistentProperty} instances. * diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java index b377f4b1e..622c0bd58 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContext.java @@ -41,17 +41,21 @@ public interface MappingContext, P extends Pers Collection getPersistentEntities(); /** - * Returns a {@link PersistentEntity} for the given {@link Class}. + * Returns a {@link PersistentEntity} for the given {@link Class}. Will return {@literal null} for types that are + * considered simple ones. * - * @param type + * @see org.springframework.data.mapping.model.SimpleTypeHolder#isSimpleType(Class) + * @param type must not be {@literal null}. * @return */ E getPersistentEntity(Class type); /** - * Returns a {@link PersistentEntity} for the given {@link TypeInformation}. + * Returns a {@link PersistentEntity} for the given {@link TypeInformation}. Will return {@literal null} for types + * that are considered simple ones. * - * @param type + * @see org.springframework.data.mapping.model.SimpleTypeHolder#isSimpleType(Class) + * @param type must not be {@literal null}. * @return */ E getPersistentEntity(TypeInformation type); @@ -61,7 +65,9 @@ public interface MappingContext, P extends Pers * * @param persistentProperty * @return the {@link PersistentEntity} mapped by the given {@link PersistentProperty} or null if no - * {@link PersistentEntity} exists for it or the {@link PersistentProperty} does not refer to an entity. + * {@link PersistentEntity} exists for it or the {@link PersistentProperty} does not refer to an entity (the + * type of the property is considered simple see + * {@link org.springframework.data.mapping.model.SimpleTypeHolder#isSimpleType(Class)}). */ E getPersistentEntity(P persistentProperty); diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java index 6c3d7db85..1bf404e5e 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/context/AbstractMappingContextUnitTests.java @@ -90,6 +90,32 @@ public class AbstractMappingContextUnitTests { verify(context, times(1)).publishEvent(Mockito.any(ApplicationEvent.class)); } + /** + * @see DATACMNS-214 + */ + @Test + public void returnsNullPersistentEntityForSimpleTypes() { + + DummyMappingContext context = new DummyMappingContext(); + assertThat(context.getPersistentEntity(String.class), is(nullValue())); + } + + /** + * @see DATACMNS-214 + */ + @Test(expected = IllegalArgumentException.class) + public void rejectsNullValueForGetPersistentEntityOfClass() { + context.getPersistentEntity((Class) null); + } + + /** + * @see DATACMNS-214 + */ + @Test(expected = IllegalArgumentException.class) + public void rejectsNullValueForGetPersistentEntityOfTypeInformation() { + context.getPersistentEntity((TypeInformation) null); + } + class Person { String name; } @@ -135,6 +161,7 @@ public class AbstractMappingContextUnitTests { return false; } + @Override protected Association createAssociation() { return new Association(this, null); }