DATACMNS-934 - BasicPersistentEntity.addAssociation(…) drops null values now.

Previously a null value was added to the list of associations if it was handed to BasicaPersistentEntity.addAssociation(…). We now drop those null values and log a warning as it usually indicates a MappingContext implementation identifying a property as association but subsequently failing to look it up.
This commit is contained in:
Oliver Gierke
2016-12-05 16:39:05 +01:00
parent 76d1995b37
commit 2377cbac85
2 changed files with 32 additions and 1 deletions

View File

@@ -30,6 +30,8 @@ import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mapping.Association;
@@ -58,7 +60,9 @@ import org.springframework.util.StringUtils;
*/
public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implements MutablePersistentEntity<T, P> {
private static final Logger LOGGER = LoggerFactory.getLogger(BasicPersistentEntity.class);
private static final String TYPE_MISMATCH = "Target bean of type %s is not of type of the persistent entity (%s)!";
private static final String NULL_ASSOCIATION = "%s.addAssociation(…) was called with a null association! Usually indicates a problem in a Spring Data MappingContext implementation. Be sure to file a bug at https://jira.spring.io!";
private final PreferredConstructor<T, P> constructor;
private final TypeInformation<T> information;
@@ -236,11 +240,17 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
return property;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.MutablePersistentEntity#addAssociation(org.springframework.data.mapping.model.Association)
*/
public void addAssociation(Association<P> association) {
if (association == null) {
LOGGER.warn(String.format(NULL_ASSOCIATION, this.getClass().getName()));
return;
}
if (!associations.contains(association)) {
associations.add(association);
}

View File

@@ -27,6 +27,7 @@ import java.util.Iterator;
import java.util.List;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -39,11 +40,13 @@ import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentEntitySpec;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.Person;
import org.springframework.data.mapping.SimpleAssociationHandler;
import org.springframework.data.mapping.context.SampleMappingContext;
import org.springframework.data.mapping.context.SamplePersistentProperty;
import org.springframework.data.util.ClassTypeInformation;
@@ -275,6 +278,24 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
entity.getPropertyAccessor(new Object());
}
/**
* @see DATACMNS-934
*/
@Test
public void doesNotThrowAnExceptionForNullAssociation() {
BasicPersistentEntity<Entity, T> entity = createEntity(Entity.class);
entity.addAssociation(null);
entity.doWithAssociations(new SimpleAssociationHandler() {
@Override
public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
Assert.fail("Expected the method to never be called!");
}
});
}
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
return createEntity(type, null);
}