From d03bfb877a1c2142a81511854c9a1fa375c773f3 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Thu, 11 Mar 2021 10:14:52 +0100 Subject: [PATCH] =?UTF-8?q?Introduce=20PersistentEntity.doWithAll(?= =?UTF-8?q?=E2=80=A6).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To let a PropertyHandler operate on all properties *and* the inverse property of associations. Fixes #2325. --- .../data/mapping/PersistentEntity.java | 17 ++++++++++++++ .../model/BasicPersistentEntityUnitTests.java | 22 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/mapping/PersistentEntity.java b/src/main/java/org/springframework/data/mapping/PersistentEntity.java index e3cfef7ee..833ee40b7 100644 --- a/src/main/java/org/springframework/data/mapping/PersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/PersistentEntity.java @@ -21,6 +21,7 @@ import java.util.Iterator; import org.springframework.data.annotation.Immutable; import org.springframework.data.util.TypeInformation; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; /** * Represents a persistent entity. @@ -240,6 +241,22 @@ public interface PersistentEntity> extends It void doWithAssociations(SimpleAssociationHandler handler); + /** + * Applies the given {@link PropertyHandler} to both all {@link PersistentProperty}s as well as all inverse properties + * of all {@link Association}s. + * + * @param handler must not be {@literal null}. + * @since 2.5 + */ + default void doWithAll(PropertyHandler

handler) { + + Assert.notNull(handler, "PropertyHandler must not be null!"); + + doWithProperties(handler); + doWithAssociations( + (AssociationHandler

) association -> handler.doWithPersistentProperty(association.getInverse())); + } + /** * Looks up the annotation of the given type on the {@link PersistentEntity}. * 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 04a6a48f3..c94c2c399 100755 --- a/src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java @@ -23,6 +23,7 @@ import lombok.RequiredArgsConstructor; import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -36,7 +37,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; - import org.springframework.core.annotation.AliasFor; import org.springframework.data.annotation.AccessType; import org.springframework.data.annotation.AccessType.Type; @@ -45,6 +45,7 @@ import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.Immutable; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.Persistent; +import org.springframework.data.annotation.Reference; import org.springframework.data.annotation.Transient; import org.springframework.data.annotation.TypeAlias; import org.springframework.data.domain.Persistable; @@ -361,6 +362,17 @@ class BasicPersistentEntityUnitTests> { .forEach(it -> assertThat(createPopulatedPersistentEntity(it).requiresPropertyPopulation()).isFalse()); } + @Test // #2325 + void doWithAllInvokesPropertyHandlerForBothAPropertiesAndAssociations() { + + PersistentEntity entity = createPopulatedPersistentEntity(WithAssociation.class); + + List seenProperties = new ArrayList<>(); + entity.doWithAll(property -> seenProperties.add(property.getName())); + + assertThat(seenProperties).containsExactlyInAnyOrder("property", "association"); + } + private BasicPersistentEntity createEntity(Class type) { return createEntity(type, null); } @@ -461,4 +473,12 @@ class BasicPersistentEntityUnitTests> { private final String firstname, lastname; private @Transient String email; } + + // #2325 + + static class WithAssociation { + + String property; + @Reference WithAssociation association; + } }