#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
@@ -809,10 +809,6 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
public void update(Object bean, Transaction t) throws OptimisticLockException {
}
@Override
public void update(Object bean, Transaction transaction, boolean deleteMissingChildren) throws OptimisticLockException {
}
@Override
public void insert(Object bean) {
}
@@ -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;
}
}
@@ -1,16 +1,17 @@
package org.tests.model.softdelete;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import java.util.List;
import static javax.persistence.CascadeType.ALL;
@Entity
public class EsdMaster extends BaseSoftDelete {
String name;
@OneToMany(mappedBy = "master", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "master", cascade = ALL, orphanRemoval = true)
List<EsdDetail> details;
public EsdMaster(String name) {
@@ -1,7 +1,7 @@
package org.tests.softdelete;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.DB;
import io.ebeantest.LoggedSql;
import org.junit.Test;
import org.tests.model.softdelete.EsdDetail;
@@ -21,8 +21,7 @@ public class TestSoftDeleteStatelessUpdate extends BaseTestCase {
master.getDetails().add(new EsdDetail("d2"));
master.getDetails().add(new EsdDetail("d3"));
Ebean.save(master);
DB.save(master);
EsdMaster upd = new EsdMaster("m1-modified");
upd.setId(master.getId());
@@ -38,7 +37,7 @@ public class TestSoftDeleteStatelessUpdate extends BaseTestCase {
LoggedSql.start();
Ebean.getDefaultServer().update(upd, null, true);
DB.update(upd);
List<String> sql = LoggedSql.collect();
assertThat(sql).hasSize(5);
@@ -47,7 +46,7 @@ public class TestSoftDeleteStatelessUpdate extends BaseTestCase {
assertThat(sql.get(1)).contains("update esd_detail set deleted=true where master_id = ? and not");
}
EsdMaster fetchedWithSoftDeletes = Ebean.find(EsdMaster.class)
EsdMaster fetchedWithSoftDeletes = DB.find(EsdMaster.class)
.setId(master.getId())
.setIncludeSoftDeletes()
.fetch("details")
@@ -59,7 +58,7 @@ public class TestSoftDeleteStatelessUpdate extends BaseTestCase {
assertThat(sql).hasSize(1);
assertThat(sql.get(0)).contains("left join esd_detail t1 on t1.master_id = t0.id where t0.id = ?");
EsdMaster fetchedWithOutSoftDeletes = Ebean.find(EsdMaster.class)
EsdMaster fetchedWithOutSoftDeletes = DB.find(EsdMaster.class)
.setId(master.getId())
.fetch("details")
.findOne();
@@ -48,7 +48,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
updateAll.setId(e.getId());
updateAll.setName("updAllProps");
server.update(updateAll, null, false);
server.update(updateAll);
eBasic = server.find(EBasic.class, e.getId());
assertEquals(e.getStatus(), eBasic.getStatus());
@@ -383,9 +383,7 @@ public class TestStatelessUpdate extends TransactionalTestCase {
updateCustomer.getContacts().add(updateContact3);
// not adding contact2 but it won't be deleted in this case
boolean deleteMissingChildren = false;
server.update(updateCustomer, null, deleteMissingChildren);
server.update(updateCustomer);
// assert
Customer assCustomer = server.find(Customer.class, customer.getId());