#1656 #1824 Stateless updates - Remove update deleteMissingChildren option, instead always use orphanRemoval

This commit is contained in:
rob bygrave
2019-09-20 17:12:32 +12:00
parent 3574479627
commit fcadc63338
23 changed files with 227 additions and 151 deletions
@@ -0,0 +1,37 @@
package org.tests.cascade;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.List;
import static javax.persistence.CascadeType.ALL;
@Entity
public class COOne {
@Id
private long id;
private final String name;
@OneToMany(cascade = ALL, orphanRemoval = true)
private List<COOneMany> children;
public COOne(String name) {
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public List<COOneMany> getChildren() {
return children;
}
}
@@ -0,0 +1,34 @@
package org.tests.cascade;
import io.ebean.annotation.SoftDelete;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class COOneMany {
@Id
private long id;
@SoftDelete
private boolean deleted;
private String name;
public COOneMany(String name) {
this.name = name;
}
public String getName() {
return name;
}
public long getId() {
return id;
}
public boolean isDeleted() {
return deleted;
}
}
@@ -0,0 +1,36 @@
package org.tests.cascade;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import static javax.persistence.CascadeType.ALL;
@Entity
public class CORoot {
@Id
private long id;
private final String name;
@OneToOne(cascade = ALL, orphanRemoval = true)
private COOne one;
public CORoot(String name, COOne one) {
this.name = name;
this.one = one;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public COOne getOne() {
return one;
}
}
@@ -0,0 +1,66 @@
package org.tests.cascade;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import io.ebean.text.json.JsonContext;
import org.junit.Test;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
public class TestCascadeOrphanStatelessUpdate extends BaseTestCase {
@Test
public void update() {
final CORoot orig = setup();
CORoot root = DB.find(CORoot.class, orig.getId());
assertNotNull(root);
assertThat(root.getOne().getChildren()).hasSize(3);
final JsonContext jsonContext = DB.json();
String asJson = jsonContext.toJson(root);
final CORoot deserialized = jsonContext.toBean(CORoot.class, asJson);
final COOne one = deserialized.getOne();
COOneMany removed0 = one.getChildren().remove(0);
COOneMany removed1 = one.getChildren().remove(1);
one.getChildren().add(new COOneMany("m3"));
one.getChildren().add(new COOneMany("m4"));
DB.update(deserialized);
CORoot saved = DB.find(CORoot.class, orig.getId());
assertNotNull(saved);
assertThat(saved.getOne().getChildren()).hasSize(3);
List<COOneMany> softDeleted = DB.find(COOneMany.class)
.setIncludeSoftDeletes()
.where()
.idIn(asList(removed0.getId(), removed1.getId()))
.findList();
assertThat(softDeleted).hasSize(2);
assertThat(softDeleted.stream().map(COOneMany::getId).collect(toList()))
.contains(removed0.getId(), removed1.getId());
}
private CORoot setup() {
COOne one = new COOne("one");
one.getChildren().add(new COOneMany("m0"));
one.getChildren().add(new COOneMany("m1"));
one.getChildren().add(new COOneMany("m2"));
CORoot root = new CORoot("r0", one);
DB.save(root);
return root;
}
}