From 9129622d1c80249c43e2f38ef62159305747da3c Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 13 Nov 2019 13:33:32 +0100 Subject: [PATCH] DATAJDBC-417 - Polishing. Reformat code. Fix collection-like node creation to cast to Iterable and consider arrays. Original pull request: #169. --- .../relational/core/conversion/WritingContext.java | 14 ++++++++++---- .../RelationalEntityWriterUnitTests.java | 6 ++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java index ece16ad0..b0dea325 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java @@ -16,7 +16,7 @@ package org.springframework.data.relational.core.conversion; import java.util.ArrayList; -import java.util.Collection; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -36,6 +36,7 @@ import org.springframework.util.Assert; * * @author Jens Schauder * @author Bastian Wilhelm + * @author Mark Paluch */ class WritingContext { @@ -116,6 +117,7 @@ class WritingContext { return actions; } + @SuppressWarnings("unchecked") private List> insertAll(PersistentPropertyPath path) { List> actions = new ArrayList<>(); @@ -126,7 +128,6 @@ class WritingContext { DbAction.Insert insert; if (node.getPath().getRequiredLeafProperty().isQualified()) { - @SuppressWarnings("unchecked") Pair value = (Pair) node.getValue(); insert = new DbAction.Insert<>(value.getSecond(), path, parentAction); insert.getQualifiers().put(node.getPath(), value.getFirst()); @@ -240,8 +241,9 @@ class WritingContext { @Nullable private Object getFromRootValue(PersistentPropertyPath path) { - if (path.getLength() == 0) + if (path.getLength() == 0) { return entity; + } Object parent = getFromRootValue(path.getParentPath()); if (parent == null) { @@ -274,7 +276,11 @@ class WritingContext { } } } else if (path.getRequiredLeafProperty().isCollectionLike()) { // collection value - ((Collection) value).forEach(v -> nodes.add(new PathNode(path, parentNode, v))); + if (value.getClass().isArray()) { + Arrays.asList((Object[]) value).forEach(v -> nodes.add(new PathNode(path, parentNode, v))); + } else { + ((Iterable) value).forEach(v -> nodes.add(new PathNode(path, parentNode, v))); + } } else { // single entity value nodes.add(new PathNode(path, parentNode, value)); } diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityWriterUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityWriterUnitTests.java index d66044d7..a6fc4d2b 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityWriterUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityWriterUnitTests.java @@ -117,7 +117,6 @@ public class RelationalEntityWriterUnitTests { ); } - @Test // DATAJDBC-112 public void newEntityWithReferenceGetsConvertedToTwoInserts() { @@ -551,6 +550,7 @@ public class RelationalEntityWriterUnitTests { tuple(InsertRoot.class, EmbeddedReferenceChainEntity.class, "", EmbeddedReferenceChainEntity.class, false) // ); } + @Test // DATAJDBC-417 public void savingInnerNullEmbeddedWithEntity() { @@ -570,7 +570,8 @@ public class RelationalEntityWriterUnitTests { DbActionTestSupport::actualEntityType, // DbActionTestSupport::isWithDependsOn) // .containsExactly( // - tuple(InsertRoot.class, RootWithEmbeddedReferenceChainEntity.class, "", RootWithEmbeddedReferenceChainEntity.class, false), // + tuple(InsertRoot.class, RootWithEmbeddedReferenceChainEntity.class, "", + RootWithEmbeddedReferenceChainEntity.class, false), // tuple(Insert.class, EmbeddedReferenceChainEntity.class, "other", EmbeddedReferenceChainEntity.class, true) // ); } @@ -636,6 +637,7 @@ public class RelationalEntityWriterUnitTests { @Id final Long id; @Embedded(onEmpty = OnEmpty.USE_NULL, prefix = "prefix_") ElementReference other; } + @RequiredArgsConstructor static class RootWithEmbeddedReferenceChainEntity {