Browse Source

Introduce PersistentEntity.doWithAll(…).

To let a PropertyHandler operate on all properties *and* the inverse property of associations.

Fixes #2325.
pull/2333/head
Oliver Drotbohm 5 years ago
parent
commit
d03bfb877a
No known key found for this signature in database
GPG Key ID: C25FBFA0DA493A1D
  1. 17
      src/main/java/org/springframework/data/mapping/PersistentEntity.java
  2. 22
      src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java

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

@ -21,6 +21,7 @@ import java.util.Iterator; @@ -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<T, P extends PersistentProperty<P>> extends It @@ -240,6 +241,22 @@ public interface PersistentEntity<T, P extends PersistentProperty<P>> 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<P> handler) {
Assert.notNull(handler, "PropertyHandler must not be null!");
doWithProperties(handler);
doWithAssociations(
(AssociationHandler<P>) association -> handler.doWithPersistentProperty(association.getInverse()));
}
/**
* Looks up the annotation of the given type on the {@link PersistentEntity}.
*

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

@ -23,6 +23,7 @@ import lombok.RequiredArgsConstructor; @@ -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; @@ -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; @@ -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<T extends PersistentProperty<T>> { @@ -361,6 +362,17 @@ class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
.forEach(it -> assertThat(createPopulatedPersistentEntity(it).requiresPropertyPopulation()).isFalse());
}
@Test // #2325
void doWithAllInvokesPropertyHandlerForBothAPropertiesAndAssociations() {
PersistentEntity<?, ?> entity = createPopulatedPersistentEntity(WithAssociation.class);
List<String> seenProperties = new ArrayList<>();
entity.doWithAll(property -> seenProperties.add(property.getName()));
assertThat(seenProperties).containsExactlyInAnyOrder("property", "association");
}
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
return createEntity(type, null);
}
@ -461,4 +473,12 @@ class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> { @@ -461,4 +473,12 @@ class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
private final String firstname, lastname;
private @Transient String email;
}
// #2325
static class WithAssociation {
String property;
@Reference WithAssociation association;
}
}

Loading…
Cancel
Save