Skip to content

Commit 2377cba

Browse files
committed
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.
1 parent 76d1995 commit 2377cba

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Set;
3131
import java.util.TreeSet;
3232

33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3335
import org.springframework.core.annotation.AnnotatedElementUtils;
3436
import org.springframework.data.annotation.TypeAlias;
3537
import org.springframework.data.mapping.Association;
@@ -58,7 +60,9 @@
5860
*/
5961
public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implements MutablePersistentEntity<T, P> {
6062

63+
private static final Logger LOGGER = LoggerFactory.getLogger(BasicPersistentEntity.class);
6164
private static final String TYPE_MISMATCH = "Target bean of type %s is not of type of the persistent entity (%s)!";
65+
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!";
6266

6367
private final PreferredConstructor<T, P> constructor;
6468
private final TypeInformation<T> information;
@@ -236,11 +240,17 @@ protected P returnPropertyIfBetterIdPropertyCandidateOrNull(P property) {
236240
return property;
237241
}
238242

239-
/* (non-Javadoc)
243+
/*
244+
* (non-Javadoc)
240245
* @see org.springframework.data.mapping.MutablePersistentEntity#addAssociation(org.springframework.data.mapping.model.Association)
241246
*/
242247
public void addAssociation(Association<P> association) {
243248

249+
if (association == null) {
250+
LOGGER.warn(String.format(NULL_ASSOCIATION, this.getClass().getName()));
251+
return;
252+
}
253+
244254
if (!associations.contains(association)) {
245255
associations.add(association);
246256
}

src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828

2929
import org.hamcrest.CoreMatchers;
30+
import org.junit.Assert;
3031
import org.junit.Rule;
3132
import org.junit.Test;
3233
import org.junit.rules.ExpectedException;
@@ -39,11 +40,13 @@
3940
import org.springframework.data.annotation.CreatedDate;
4041
import org.springframework.data.annotation.LastModifiedBy;
4142
import org.springframework.data.annotation.TypeAlias;
43+
import org.springframework.data.mapping.Association;
4244
import org.springframework.data.mapping.PersistentEntity;
4345
import org.springframework.data.mapping.PersistentEntitySpec;
4446
import org.springframework.data.mapping.PersistentProperty;
4547
import org.springframework.data.mapping.PersistentPropertyAccessor;
4648
import org.springframework.data.mapping.Person;
49+
import org.springframework.data.mapping.SimpleAssociationHandler;
4750
import org.springframework.data.mapping.context.SampleMappingContext;
4851
import org.springframework.data.mapping.context.SamplePersistentProperty;
4952
import org.springframework.data.util.ClassTypeInformation;
@@ -275,6 +278,24 @@ public void invalidBeanAccessCreatesDescriptiveErrorMessage() {
275278
entity.getPropertyAccessor(new Object());
276279
}
277280

281+
/**
282+
* @see DATACMNS-934
283+
*/
284+
@Test
285+
public void doesNotThrowAnExceptionForNullAssociation() {
286+
287+
BasicPersistentEntity<Entity, T> entity = createEntity(Entity.class);
288+
entity.addAssociation(null);
289+
290+
entity.doWithAssociations(new SimpleAssociationHandler() {
291+
292+
@Override
293+
public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
294+
Assert.fail("Expected the method to never be called!");
295+
}
296+
});
297+
}
298+
278299
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
279300
return createEntity(type, null);
280301
}

0 commit comments

Comments
 (0)