Skip to content

Commit

Permalink
fix: handle null primary keys between parent and child objects (#3179)
Browse files Browse the repository at this point in the history
* fix: handle null primary keys between parent and child objects
  • Loading branch information
ldetmer committed Sep 4, 2024
1 parent 02a1e65 commit 42c69b6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
Expand Down Expand Up @@ -163,7 +164,7 @@ private void verifyChildHasParentId(
Iterator childKeyParts = this.spannerSchemaUtils.getKey(childObject).getParts().iterator();
int partNum = 1;
while (parentKeyParts.hasNext()) {
if (!childKeyParts.hasNext() || !parentKeyParts.next().equals(childKeyParts.next())) {
if (!childKeyParts.hasNext() || !Objects.equals(parentKeyParts.next(), childKeyParts.next())) {
throw new SpannerDataException(
"A child entity's common primary key parts with its parent must "
+ "have the same values. Primary key component "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ void insertChildrenMismatchIdTest() {
+ " com.google.cloud.spring.data.spanner.core.SpannerMutationFactoryImplTests$ChildEntity");
}

@Test
void insertChildrenNullIdTest() {
Parent parent = new Parent();
parent.keyOne = "a";
Child child = new Child();
child.keyOne = "a";
child.keyThree = 3L;
parent.children = Collections.singletonList(child);

List<Mutation> mutations = this.spannerMutationFactory.insert(parent);
assertThat(mutations).hasSize(2);
}

@Test
void updateTest() {
executeWriteTest(t -> this.spannerMutationFactory.update(t, null), Op.UPDATE);
Expand Down Expand Up @@ -270,4 +283,33 @@ private static class EmbeddedKeyComponents {
@PrimaryKey(keyOrder = 2)
String id2;
}

@Table
private static class Parent {
@PrimaryKey
@Column
String keyOne;

@PrimaryKey(keyOrder = 2)
@Column
Long keyTwo;

@Interleaved
List<Child> children;
}

@Table
private static class Child {
@PrimaryKey
@Column
String keyOne;

@PrimaryKey(keyOrder = 2)
@Column
Long keyTwo;

@PrimaryKey(keyOrder = 3)
@Column
Long keyThree;
}
}

0 comments on commit 42c69b6

Please sign in to comment.