Skip to content

Commit ac58399

Browse files
committed
Polishing.
Refactoring and code aesthetics. See #1249 Original pull request #1250
1 parent dcd4ef0 commit ac58399

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
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

+4-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,8 @@ 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).toList();
6768
}
6869

6970
/**

spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityDeleteWriterUnitTests.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package org.springframework.data.relational.core.conversion;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
1820
import lombok.Data;
21+
import lombok.RequiredArgsConstructor;
1922

2023
import java.util.ArrayList;
2124
import java.util.List;
2225

23-
import lombok.RequiredArgsConstructor;
24-
import org.assertj.core.api.Assertions;
2526
import org.assertj.core.groups.Tuple;
2627
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.api.extension.ExtendWith;
@@ -57,7 +58,7 @@ public void deleteDeletesTheEntityAndReferencedEntities() {
5758

5859
converter.write(entity.id, aggregateChange);
5960

60-
Assertions.assertThat(extractActions(aggregateChange))
61+
assertThat(extractActions(aggregateChange))
6162
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
6263
.containsExactly( //
6364
Tuple.tuple(AcquireLockRoot.class, SomeEntity.class, ""), //
@@ -76,7 +77,7 @@ public void deleteDeletesTheEntityAndNoReferencedEntities() {
7677

7778
converter.write(entity.id, aggregateChange);
7879

79-
Assertions.assertThat(extractActions(aggregateChange))
80+
assertThat(extractActions(aggregateChange))
8081
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
8182
.containsExactly(Tuple.tuple(DeleteRoot.class, SingleEntity.class, ""));
8283
}
@@ -88,7 +89,7 @@ public void deleteAllDeletesAllEntitiesAndReferencedEntities() {
8889

8990
converter.write(null, aggregateChange);
9091

91-
Assertions.assertThat(extractActions(aggregateChange))
92+
assertThat(extractActions(aggregateChange))
9293
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
9394
.containsExactly( //
9495
Tuple.tuple(AcquireLockAllRoot.class, SomeEntity.class, ""), //
@@ -105,7 +106,7 @@ public void deleteAllDeletesAllEntitiesAndNoReferencedEntities() {
105106

106107
converter.write(null, aggregateChange);
107108

108-
Assertions.assertThat(extractActions(aggregateChange))
109+
assertThat(extractActions(aggregateChange))
109110
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
110111
.containsExactly(Tuple.tuple(DeleteAllRoot.class, SingleEntity.class, ""));
111112
}
@@ -115,11 +116,12 @@ public void deleteDoesNotDeleteReadOnlyReferences() {
115116

116117
WithReadOnlyReference entity = new WithReadOnlyReference(23L);
117118

118-
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange.forDelete(WithReadOnlyReference.class);
119+
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange
120+
.forDelete(WithReadOnlyReference.class);
119121

120122
converter.write(entity.id, aggregateChange);
121123

122-
Assertions.assertThat(extractActions(aggregateChange))
124+
assertThat(extractActions(aggregateChange))
123125
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
124126
.containsExactly( //
125127
Tuple.tuple(DeleteRoot.class, WithReadOnlyReference.class, "") //
@@ -131,18 +133,18 @@ public void deleteAllDoesNotDeleteReadOnlyReferences() {
131133

132134
WithReadOnlyReference entity = new WithReadOnlyReference(23L);
133135

134-
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange.forDelete(WithReadOnlyReference.class);
136+
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange
137+
.forDelete(WithReadOnlyReference.class);
135138

136139
converter.write(null, aggregateChange);
137140

138-
Assertions.assertThat(extractActions(aggregateChange))
141+
assertThat(extractActions(aggregateChange))
139142
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
140143
.containsExactly( //
141144
Tuple.tuple(DeleteAllRoot.class, WithReadOnlyReference.class, "") //
142145
);
143146
}
144147

145-
146148
private List<DbAction<?>> extractActions(MutableAggregateChange<?> aggregateChange) {
147149

148150
List<DbAction<?>> actions = new ArrayList<>();
@@ -179,8 +181,8 @@ private class SingleEntity {
179181

180182
@RequiredArgsConstructor
181183
private static class WithReadOnlyReference {
184+
182185
@Id final Long id;
183-
@ReadOnlyProperty
184-
OtherEntity other;
186+
@ReadOnlyProperty OtherEntity other;
185187
}
186188
}

spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityInsertWriterUnitTests.java

-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.junit.jupiter.api.extension.ExtendWith;
2727
import org.mockito.junit.jupiter.MockitoExtension;
2828
import org.springframework.data.annotation.Id;
29-
import org.springframework.data.annotation.ReadOnlyProperty;
3029
import org.springframework.data.relational.core.conversion.DbAction.InsertRoot;
3130
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3231

@@ -73,10 +72,8 @@ public void existingEntityGetsNotConvertedToDeletePlusUpdate() {
7372
.containsExactly( //
7473
tuple(InsertRoot.class, SingleReferenceEntity.class, "", SingleReferenceEntity.class, false) //
7574
);
76-
7775
}
7876

79-
8077
private List<DbAction<?>> extractActions(MutableAggregateChange<?> aggregateChange) {
8178

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

0 commit comments

Comments
 (0)