From d3f235872ceb5770b17273a5bcf581fe061a7f07 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 5 Dec 2016 16:39:05 +0100 Subject: [PATCH] =?UTF-8?q?DATACMNS-934=20-=20BasicPersistentEntity.addAss?= =?UTF-8?q?ociation(=E2=80=A6)=20drops=20null=20values=20now.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../mapping/model/BasicPersistentEntity.java | 12 ++++++++++- .../model/BasicPersistentEntityUnitTests.java | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index e408065e4..c1c23fb62 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -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> implements MutablePersistentEntity { + 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 constructor; private final TypeInformation information; @@ -236,11 +240,17 @@ public class BasicPersistentEntity> implement return property; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see org.springframework.data.mapping.MutablePersistentEntity#addAssociation(org.springframework.data.mapping.model.Association) */ public void addAssociation(Association

association) { + if (association == null) { + LOGGER.warn(String.format(NULL_ASSOCIATION, this.getClass().getName())); + return; + } + if (!associations.contains(association)) { associations.add(association); } diff --git a/src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java b/src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java index 5330b73be..48a52df92 100644 --- a/src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java @@ -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> { entity.getPropertyAccessor(new Object()); } + /** + * @see DATACMNS-934 + */ + @Test + public void doesNotThrowAnExceptionForNullAssociation() { + + BasicPersistentEntity entity = createEntity(Entity.class); + entity.addAssociation(null); + + entity.doWithAssociations(new SimpleAssociationHandler() { + + @Override + public void doWithAssociation(Association> association) { + Assert.fail("Expected the method to never be called!"); + } + }); + } + private BasicPersistentEntity createEntity(Class type) { return createEntity(type, null); }