DATAJDBC-417 - Polishing.

Reformat code. Fix collection-like node creation to cast to Iterable and consider arrays.

Original pull request: #169.
This commit is contained in:
Mark Paluch
2019-11-13 13:33:32 +01:00
parent cf9b480ea2
commit 9129622d1c
2 changed files with 14 additions and 6 deletions

View File

@@ -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<DbAction<?>> insertAll(PersistentPropertyPath<RelationalPersistentProperty> path) {
List<DbAction<?>> actions = new ArrayList<>();
@@ -126,7 +128,6 @@ class WritingContext {
DbAction.Insert<Object> insert;
if (node.getPath().getRequiredLeafProperty().isQualified()) {
@SuppressWarnings("unchecked")
Pair<Object, Object> 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<RelationalPersistentProperty> 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));
}

View File

@@ -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 {