diff --git a/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java b/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java
index aa27d8502..6e4059989 100644
--- a/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java
+++ b/src/main/java/io/ebeaninternal/server/persist/DefaultPersister.java
@@ -949,20 +949,15 @@ public final class DefaultPersister implements Persister {
Set> modifyRemovals = c.getModifyRemovals();
saveMany.modifyListenReset(c);
if (modifyRemovals != null && !modifyRemovals.isEmpty()) {
-
- SpiTransaction t = saveMany.getTransaction();
- // increase depth for batching order
- t.depth(+1);
for (Object removedBean : modifyRemovals) {
if (removedBean instanceof EntityBean) {
EntityBean eb = (EntityBean) removedBean;
if (eb._ebean_getIntercept().isLoaded()) {
// only delete if the bean was loaded meaning that it is known to exist in the DB
- deleteRequest(createPublishRequest(removedBean, t, PersistRequest.Type.DELETE, saveMany.isPublish()));
+ deleteRequest(createPublishRequest(removedBean, saveMany.getTransaction(), PersistRequest.Type.DELETE, saveMany.isPublish()));
}
}
}
- t.depth(-1);
}
}
}
@@ -992,21 +987,23 @@ public final class DefaultPersister implements Persister {
targetDescriptor.preAllocateIds(collection.size());
}
- ArrayList detailIds = null;
+ SpiTransaction t = saveMany.getTransaction();
+ boolean isMap = ManyType.MAP.equals(prop.getManyType());
+ EntityBean parentBean = saveMany.getParentBean();
+
if (deleteMissingChildren) {
// collect the Id's (to exclude from deleteManyDetails)
- detailIds = new ArrayList<>();
+ List detailIds = collectIds(collection, targetDescriptor, isMap);
+ // deleting missing children - children not in our collected detailIds
+ deleteManyDetails(t, prop.getBeanDescriptor(), parentBean, prop, detailIds, false);
}
// increase depth for batching order
- SpiTransaction t = saveMany.getTransaction();
t.depth(+1);
// if a map, then we get the key value and
// set it to the appropriate property on the
// detail bean before we save it
- boolean isMap = ManyType.MAP.equals(prop.getManyType());
- EntityBean parentBean = saveMany.getParentBean();
Object mapKeyValue = null;
boolean saveSkippable = prop.isSaveRecurseSkippable();
@@ -1047,28 +1044,30 @@ public final class DefaultPersister implements Persister {
}
}
- if (detailIds != null) {
- // stateless update with deleteMissingChildren so first
- // flushBatch to ensure that we have Id values from any inserts
- t.flushBatch();
- // now collect the Id values to determine the 'missing children'
- for (Object detailBean : collection) {
- if (isMap) {
- detailBean = ((Map.Entry, ?>) detailBean).getValue();
- }
- if (detailBean instanceof EntityBean) {
- Object id = targetDescriptor.getId((EntityBean) detailBean);
- if (!DmlUtil.isNullOrZero(id)) {
- // remember the Id (other details not in the collection) will be removed
- detailIds.add(id);
- }
+ t.depth(-1);
+ }
+
+ /**
+ * Collect the Id values of the details to remove 'missing children' for stateless updates.
+ */
+ private List collectIds(Collection> collection, BeanDescriptor> targetDescriptor, boolean isMap) {
+
+ List detailIds = new ArrayList<>();
+ // stateless update with deleteMissingChildren so first
+ // collect the Id values to remove the 'missing children'
+ for (Object detailBean : collection) {
+ if (isMap) {
+ detailBean = ((Map.Entry, ?>) detailBean).getValue();
+ }
+ if (detailBean instanceof EntityBean) {
+ Object id = targetDescriptor.getId((EntityBean) detailBean);
+ if (!DmlUtil.isNullOrZero(id)) {
+ // remember the Id (other details not in the collection) will be removed
+ detailIds.add(id);
}
}
- // deleting missing children - children not in our collected detailIds
- deleteManyDetails(t, prop.getBeanDescriptor(), parentBean, prop, detailIds, false);
}
-
- t.depth(-1);
+ return detailIds;
}
/**
@@ -1268,7 +1267,7 @@ public final class DefaultPersister implements Persister {
*
*/
private void deleteManyDetails(SpiTransaction t, BeanDescriptor> desc, EntityBean parentBean,
- BeanPropertyAssocMany> many, ArrayList excludeDetailIds, boolean softDelete) {
+ BeanPropertyAssocMany> many, List excludeDetailIds, boolean softDelete) {
if (many.getCascadeInfo().isDelete()) {
// cascade delete the beans in the collection
diff --git a/src/test/java/org/tests/cascade/TestPrivateOwnedCascadeOrder.java b/src/test/java/org/tests/cascade/TestPrivateOwnedCascadeOrder.java
new file mode 100644
index 000000000..10cdbdafb
--- /dev/null
+++ b/src/test/java/org/tests/cascade/TestPrivateOwnedCascadeOrder.java
@@ -0,0 +1,30 @@
+package org.tests.cascade;
+
+import io.ebean.BaseTestCase;
+import io.ebean.Ebean;
+import org.junit.Test;
+import org.tests.model.basic.TSDetail;
+import org.tests.model.basic.TSMaster;
+
+public class TestPrivateOwnedCascadeOrder extends BaseTestCase {
+
+ @Test
+ public void test() {
+
+ // setup
+ TSMaster master = new TSMaster();
+ master.getDetails().add(new TSDetail("c97", "ONE"));
+ master.getDetails().add(new TSDetail("c96", "TWO"));
+
+ Ebean.save(master);
+
+ // act
+ TSMaster master1 = Ebean.find(master.getClass(), master.getId());
+
+ // Check Ebean deletes the existing c97 first as the unique values clash
+ master1.getDetails().clear();
+ master1.getDetails().add(new TSDetail("c98", "TWO"));
+
+ Ebean.save(master1);
+ }
+}
diff --git a/src/test/java/org/tests/model/basic/TSDetail.java b/src/test/java/org/tests/model/basic/TSDetail.java
index 5f02a1dfc..b89ec11f4 100644
--- a/src/test/java/org/tests/model/basic/TSDetail.java
+++ b/src/test/java/org/tests/model/basic/TSDetail.java
@@ -1,5 +1,7 @@
package org.tests.model.basic;
+import io.ebean.annotation.Index;
+
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@@ -23,6 +25,9 @@ public class TSDetail {
String description;
+ @Index(unique = true)
+ String someUniqueValue;
+
boolean active;
@ManyToOne
@@ -32,6 +37,11 @@ public class TSDetail {
this.name = name;
}
+ public TSDetail(String name, String someUniqueValue) {
+ this.name = name;
+ this.someUniqueValue = someUniqueValue;
+ }
+
public TSDetail() {
}
@@ -60,6 +70,14 @@ public class TSDetail {
this.description = description;
}
+ public String getSomeUniqueValue() {
+ return someUniqueValue;
+ }
+
+ public void setSomeUniqueValue(String someUniqueValue) {
+ this.someUniqueValue = someUniqueValue;
+ }
+
public boolean isActive() {
return active;
}