Skip to content

Commit 496be09

Browse files
committed
Polishing.
Refactoring. See #1249
1 parent 198d7ae commit 496be09

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityDeleteWriter.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
import java.util.ArrayList;
1919
import java.util.Collections;
2020
import java.util.List;
21+
import java.util.function.Consumer;
2122

2223
import org.springframework.data.convert.EntityWriter;
23-
import org.springframework.data.mapping.PersistentProperty;
24+
import org.springframework.data.mapping.PersistentPropertyPath;
2425
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
2526
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
26-
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
2727
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2828
import org.springframework.lang.Nullable;
2929
import org.springframework.util.Assert;
@@ -74,10 +74,7 @@ private List<DbAction<?>> deleteAll(Class<?> entityType) {
7474

7575
List<DbAction<?>> deleteReferencedActions = new ArrayList<>();
7676

77-
context.findPersistentPropertyPaths(entityType, PersistentProperty::isEntity) //
78-
.filter(p -> !p.getRequiredLeafProperty().isEmbedded() //
79-
&& PersistentPropertyPathExtension.isWritable(p)) //
80-
.forEach(p -> deleteReferencedActions.add(new DbAction.DeleteAll<>(p)));
77+
forAllTableRepresentingPaths(entityType, p -> deleteReferencedActions.add(new DbAction.DeleteAll<>(p)));
8178

8279
Collections.reverse(deleteReferencedActions);
8380

@@ -118,14 +115,18 @@ private List<DbAction<?>> deleteReferencedEntities(Object id, AggregateChange<?>
118115

119116
List<DbAction<?>> actions = new ArrayList<>();
120117

121-
context.findPersistentPropertyPaths(aggregateChange.getEntityType(), p -> p.isEntity()) //
122-
.filter(p -> !p.getRequiredLeafProperty().isEmbedded() //
123-
&& PersistentPropertyPathExtension.isWritable(p)) //
124-
.forEach(p -> actions.add(new DbAction.Delete<>(id, p)));
118+
forAllTableRepresentingPaths(aggregateChange.getEntityType(), p -> actions.add(new DbAction.Delete<>(id, p)));
125119

126120
Collections.reverse(actions);
127121

128122
return actions;
129123
}
130124

125+
private void forAllTableRepresentingPaths(Class<?> entityType,
126+
Consumer<PersistentPropertyPath<RelationalPersistentProperty>> pathConsumer) {
127+
128+
context.findPersistentPropertyPaths(entityType, property -> property.isEntity() && !property.isEmbedded()) //
129+
.filter(PersistentPropertyPathExtension::isWritable) //
130+
.forEach(pathConsumer);
131+
}
131132
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.stream.Collectors;
2626

2727
import org.springframework.data.mapping.PersistentPropertyPath;
28-
import org.springframework.data.mapping.PersistentPropertyPaths;
28+
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
2929
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3030
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3131
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
@@ -47,7 +47,7 @@ class WritingContext<T> {
4747
private final RelationalMappingContext context;
4848
private final T root;
4949
private final Class<T> entityType;
50-
private final PersistentPropertyPaths<?, RelationalPersistentProperty> paths;
50+
private final List<PersistentPropertyPath<RelationalPersistentProperty>> paths;
5151
private final Map<PathNode, DbAction<?>> previousActions = new HashMap<>();
5252
private final Map<PersistentPropertyPath<RelationalPersistentProperty>, List<PathNode>> nodesCache = new HashMap<>();
5353
private final IdValueSource rootIdValueSource;
@@ -63,7 +63,9 @@ class WritingContext<T> {
6363
this.aggregateChange = aggregateChange;
6464
this.rootIdValueSource = IdValueSource.forInstance(root,
6565
context.getRequiredPersistentEntity(aggregateChange.getEntityType()));
66-
this.paths = context.findPersistentPropertyPaths(entityType, (p) -> p.isEntity() && !p.isEmbedded() && p.isWritable());
66+
this.paths = context.findPersistentPropertyPaths(entityType, (p) -> p.isEntity() && !p.isEmbedded()) //
67+
.filter(PersistentPropertyPathExtension::isWritable) //
68+
.stream().toList();
6769
}
6870

6971
/**

0 commit comments

Comments
 (0)