Skip to content

Commit d03bfb8

Browse files
committed
Introduce PersistentEntity.doWithAll(…).
To let a PropertyHandler operate on all properties *and* the inverse property of associations. Fixes #2325.
1 parent 6b0292c commit d03bfb8

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/main/java/org/springframework/data/mapping/PersistentEntity.java

+17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.annotation.Immutable;
2222
import org.springframework.data.util.TypeInformation;
2323
import org.springframework.lang.Nullable;
24+
import org.springframework.util.Assert;
2425

2526
/**
2627
* Represents a persistent entity.
@@ -240,6 +241,22 @@ default P getPersistentProperty(Class<? extends Annotation> annotationType) {
240241

241242
void doWithAssociations(SimpleAssociationHandler handler);
242243

244+
/**
245+
* Applies the given {@link PropertyHandler} to both all {@link PersistentProperty}s as well as all inverse properties
246+
* of all {@link Association}s.
247+
*
248+
* @param handler must not be {@literal null}.
249+
* @since 2.5
250+
*/
251+
default void doWithAll(PropertyHandler<P> handler) {
252+
253+
Assert.notNull(handler, "PropertyHandler must not be null!");
254+
255+
doWithProperties(handler);
256+
doWithAssociations(
257+
(AssociationHandler<P>) association -> handler.doWithPersistentProperty(association.getInverse()));
258+
}
259+
243260
/**
244261
* Looks up the annotation of the given type on the {@link PersistentEntity}.
245262
*

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Annotation;
2424
import java.lang.annotation.Retention;
2525
import java.lang.annotation.RetentionPolicy;
26+
import java.util.ArrayList;
2627
import java.util.Comparator;
2728
import java.util.Iterator;
2829
import java.util.List;
@@ -36,7 +37,6 @@
3637
import org.mockito.Mock;
3738
import org.mockito.Mockito;
3839
import org.mockito.junit.jupiter.MockitoExtension;
39-
4040
import org.springframework.core.annotation.AliasFor;
4141
import org.springframework.data.annotation.AccessType;
4242
import org.springframework.data.annotation.AccessType.Type;
@@ -45,6 +45,7 @@
4545
import org.springframework.data.annotation.Immutable;
4646
import org.springframework.data.annotation.LastModifiedBy;
4747
import org.springframework.data.annotation.Persistent;
48+
import org.springframework.data.annotation.Reference;
4849
import org.springframework.data.annotation.Transient;
4950
import org.springframework.data.annotation.TypeAlias;
5051
import org.springframework.data.domain.Persistable;
@@ -361,6 +362,17 @@ void exposesPropertyPopulationNotRequired() {
361362
.forEach(it -> assertThat(createPopulatedPersistentEntity(it).requiresPropertyPopulation()).isFalse());
362363
}
363364

365+
@Test // #2325
366+
void doWithAllInvokesPropertyHandlerForBothAPropertiesAndAssociations() {
367+
368+
PersistentEntity<?, ?> entity = createPopulatedPersistentEntity(WithAssociation.class);
369+
370+
List<String> seenProperties = new ArrayList<>();
371+
entity.doWithAll(property -> seenProperties.add(property.getName()));
372+
373+
assertThat(seenProperties).containsExactlyInAnyOrder("property", "association");
374+
}
375+
364376
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
365377
return createEntity(type, null);
366378
}
@@ -461,4 +473,12 @@ private static class PropertyPopulationNotRequiredWithTransient {
461473
private final String firstname, lastname;
462474
private @Transient String email;
463475
}
476+
477+
// #2325
478+
479+
static class WithAssociation {
480+
481+
String property;
482+
@Reference WithAssociation association;
483+
}
464484
}

0 commit comments

Comments
 (0)