Skip to content

Commit 9129622

Browse files
committed
DATAJDBC-417 - Polishing.
Reformat code. Fix collection-like node creation to cast to Iterable and consider arrays. Original pull request: #169.
1 parent cf9b480 commit 9129622

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.springframework.data.relational.core.conversion;
1717

1818
import java.util.ArrayList;
19-
import java.util.Collection;
19+
import java.util.Arrays;
2020
import java.util.Collections;
2121
import java.util.HashMap;
2222
import java.util.List;
@@ -36,6 +36,7 @@
3636
*
3737
* @author Jens Schauder
3838
* @author Bastian Wilhelm
39+
* @author Mark Paluch
3940
*/
4041
class WritingContext {
4142

@@ -116,6 +117,7 @@ private List<DbAction<?>> insertReferenced() {
116117
return actions;
117118
}
118119

120+
@SuppressWarnings("unchecked")
119121
private List<DbAction<?>> insertAll(PersistentPropertyPath<RelationalPersistentProperty> path) {
120122

121123
List<DbAction<?>> actions = new ArrayList<>();
@@ -126,7 +128,6 @@ private List<DbAction<?>> insertAll(PersistentPropertyPath<RelationalPersistentP
126128
DbAction.Insert<Object> insert;
127129
if (node.getPath().getRequiredLeafProperty().isQualified()) {
128130

129-
@SuppressWarnings("unchecked")
130131
Pair<Object, Object> value = (Pair) node.getValue();
131132
insert = new DbAction.Insert<>(value.getSecond(), path, parentAction);
132133
insert.getQualifiers().put(node.getPath(), value.getFirst());
@@ -240,8 +241,9 @@ private boolean isDirectlyReferencedByRootIgnoringEmbeddables(
240241
@Nullable
241242
private Object getFromRootValue(PersistentPropertyPath<RelationalPersistentProperty> path) {
242243

243-
if (path.getLength() == 0)
244+
if (path.getLength() == 0) {
244245
return entity;
246+
}
245247

246248
Object parent = getFromRootValue(path.getParentPath());
247249
if (parent == null) {
@@ -274,7 +276,11 @@ private List<PathNode> createNodes(PersistentPropertyPath<RelationalPersistentPr
274276
}
275277
}
276278
} else if (path.getRequiredLeafProperty().isCollectionLike()) { // collection value
277-
((Collection<?>) value).forEach(v -> nodes.add(new PathNode(path, parentNode, v)));
279+
if (value.getClass().isArray()) {
280+
Arrays.asList((Object[]) value).forEach(v -> nodes.add(new PathNode(path, parentNode, v)));
281+
} else {
282+
((Iterable<?>) value).forEach(v -> nodes.add(new PathNode(path, parentNode, v)));
283+
}
278284
} else { // single entity value
279285
nodes.add(new PathNode(path, parentNode, value));
280286
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public void newEntityGetsConvertedToOneInsertByEmbeddedEntities() {
117117
);
118118
}
119119

120-
121120
@Test // DATAJDBC-112
122121
public void newEntityWithReferenceGetsConvertedToTwoInserts() {
123122

@@ -551,6 +550,7 @@ public void savingANullEmbeddedWithEntity() {
551550
tuple(InsertRoot.class, EmbeddedReferenceChainEntity.class, "", EmbeddedReferenceChainEntity.class, false) //
552551
);
553552
}
553+
554554
@Test // DATAJDBC-417
555555
public void savingInnerNullEmbeddedWithEntity() {
556556

@@ -570,7 +570,8 @@ public void savingInnerNullEmbeddedWithEntity() {
570570
DbActionTestSupport::actualEntityType, //
571571
DbActionTestSupport::isWithDependsOn) //
572572
.containsExactly( //
573-
tuple(InsertRoot.class, RootWithEmbeddedReferenceChainEntity.class, "", RootWithEmbeddedReferenceChainEntity.class, false), //
573+
tuple(InsertRoot.class, RootWithEmbeddedReferenceChainEntity.class, "",
574+
RootWithEmbeddedReferenceChainEntity.class, false), //
574575
tuple(Insert.class, EmbeddedReferenceChainEntity.class, "other", EmbeddedReferenceChainEntity.class, true) //
575576
);
576577
}
@@ -636,6 +637,7 @@ static class EmbeddedReferenceChainEntity {
636637
@Id final Long id;
637638
@Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_") ElementReference other;
638639
}
640+
639641
@RequiredArgsConstructor
640642
static class RootWithEmbeddedReferenceChainEntity {
641643

0 commit comments

Comments
 (0)