DATACMNS-214 - Simple types are not considered PersistentEntities anymore.
Before actually adding a type as PersistentEntity, AbstractMappingContext will call shouldCreatePersistentEntityFor(TypeInformation<?> type) now. The default implementation will just consider the SimpleTypeHolder for that decision. However as adding a Converter to the context will render the converted object to be treated as simple, one might want to override this method to be more precise and only rule out store-specific simple types at this stage.
This commit is contained in:
@@ -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<E extends MutablePersistentEntity<?
|
||||
* @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(java.lang.Class)
|
||||
*/
|
||||
public E getPersistentEntity(Class<?> type) {
|
||||
Assert.notNull(type);
|
||||
return getPersistentEntity(ClassTypeInformation.from(type));
|
||||
}
|
||||
|
||||
@@ -164,6 +166,8 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
*/
|
||||
public E getPersistentEntity(TypeInformation<?> type) {
|
||||
|
||||
Assert.notNull(type);
|
||||
|
||||
try {
|
||||
read.lock();
|
||||
E entity = persistentEntities.get(type);
|
||||
@@ -180,6 +184,10 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
throw new MappingException("Unknown persistent entity " + type);
|
||||
}
|
||||
|
||||
if (!shouldCreatePersistentEntityFor(type)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return addPersistentEntity(type);
|
||||
}
|
||||
|
||||
@@ -236,7 +244,6 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
* @return
|
||||
*/
|
||||
protected E addPersistentEntity(Class<?> type) {
|
||||
|
||||
return addPersistentEntity(ClassTypeInformation.from(type));
|
||||
}
|
||||
|
||||
@@ -345,6 +352,19 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a {@link PersistentEntity} instance should be created for the given {@link TypeInformation}. By
|
||||
* default this will reject this for all types considered simple, but it might be necessary to tweak that in case you
|
||||
* have registered custom converters for top level types (which renders them to be considered simple) but still need
|
||||
* meta-information about them.
|
||||
*
|
||||
* @param type will never be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
protected boolean shouldCreatePersistentEntityFor(TypeInformation<?> type) {
|
||||
return !simpleTypeHolder.isSimpleType(type.getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link FieldCallback} to create {@link PersistentProperty} instances.
|
||||
*
|
||||
|
||||
@@ -41,17 +41,21 @@ public interface MappingContext<E extends PersistentEntity<?, P>, P extends Pers
|
||||
Collection<E> 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<E extends PersistentEntity<?, P>, 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);
|
||||
|
||||
|
||||
@@ -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<DummyPersistenProperty> createAssociation() {
|
||||
return new Association<DummyPersistenProperty>(this, null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user