Merge pull request #2960 from ebean-orm/buchtajz-one2many_setArraylist

Fix for @OneToMany orphanRemoval not occurring for #2952 #2953
This commit is contained in:
Rob Bygrave authored and GitHub committed 2023-02-10 13:01:55 +13:00
commit 889c0f12e0
5 files changed
+101 -17

No files matched your search

@@ -38,13 +38,10 @@ public final class BeanCollectionUtil {
// returns a collection of beans // returns a collection of beans
return bc.getActualEntries(); return bc.getActualEntries();
} }
if (o instanceof Collection<?>) {
if (o instanceof Map<?, ?>) {
// yes, we want the entrySet (to set the keys)
return ((Map<?, ?>) o).entrySet();
} else if (o instanceof Collection<?>) {
return ((Collection<?>) o); return ((Collection<?>) o);
} else if (o instanceof Map<?, ?>) {
return ((Map<?, ?>) o).entrySet();
} }
throw new PersistenceException("expecting a Map or Collection but got " + o.getClass().getName()); throw new PersistenceException("expecting a Map or Collection but got " + o.getClass().getName());
} }
@@ -37,6 +37,7 @@ final class SaveManyBeans extends SaveManyBase {
private final boolean hasOrderColumn; private final boolean hasOrderColumn;
private int sortOrder; private int sortOrder;
private boolean insertAllChildren; private boolean insertAllChildren;
private boolean forceOrphanRemoval;
SaveManyBeans(DefaultPersister persister, boolean insertedParent, BeanPropertyAssocMany<?> many, EntityBean parentBean, PersistRequestBean<?> request) { SaveManyBeans(DefaultPersister persister, boolean insertedParent, BeanPropertyAssocMany<?> many, EntityBean parentBean, PersistRequestBean<?> request) {
super(persister, insertedParent, many, parentBean, request); super(persister, insertedParent, many, parentBean, request);
@@ -141,7 +142,7 @@ final class SaveManyBeans extends SaveManyBase {
} }
private boolean forcedUpdateOrphanRemoval() { private boolean forcedUpdateOrphanRemoval() {
return !insertedParent && many.isOrphanRemoval() && request.isForcedUpdate(); return !insertedParent && many.isOrphanRemoval() && (forceOrphanRemoval || request.isForcedUpdate());
} }
private void saveAllBeans(final BeanProperty orderColumn) { private void saveAllBeans(final BeanProperty orderColumn) {
@@ -228,8 +229,10 @@ final class SaveManyBeans extends SaveManyBase {
if (detailBean instanceof EntityBean) { if (detailBean instanceof EntityBean) {
Object id = targetDescriptor.id(detailBean); Object id = targetDescriptor.id(detailBean);
if (!isNullOrZero(id)) { if (!isNullOrZero(id)) {
// remember the Id (other details not in the collection) will be removed if (!forceOrphanRemoval || !((EntityBean)detailBean)._ebean_getIntercept().isNew()) {
detailIds.add(id); // remember the Id (other details not in the collection) will be removed
detailIds.add(id);
}
} }
} }
} }
@@ -359,7 +362,9 @@ final class SaveManyBeans extends SaveManyBase {
if (!hasOrderColumn) { if (!hasOrderColumn) {
c.modifyReset(); c.modifyReset();
} }
if (modifyRemovals != null && !modifyRemovals.isEmpty()) { if (modifyRemovals == null || modifyRemovals.isEmpty()) {
forceOrphanRemoval = !insertedParent && isChangedProperty();
} else {
for (Object removedBean : modifyRemovals) { for (Object removedBean : modifyRemovals) {
if (removedBean instanceof EntityBean) { if (removedBean instanceof EntityBean) {
EntityBean eb = (EntityBean) removedBean; EntityBean eb = (EntityBean) removedBean;
@@ -5,10 +5,7 @@ import io.ebean.DB;
import io.ebean.test.LoggedSql; import io.ebean.test.LoggedSql;
import io.ebean.xtest.BaseTestCase; import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.tests.o2m.dm.GoodsEntity; import org.tests.o2m.dm.*;
import org.tests.o2m.dm.PersonEntity;
import org.tests.o2m.dm.WorkflowEntity;
import org.tests.o2m.dm.WorkflowOperationEntity;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.List; import java.util.List;
@@ -235,4 +232,63 @@ class TestOneToManyStatelessUpdateResultsInSoftDelete extends BaseTestCase {
assertThat(readGoods.getWorkflowEntity().getOperations()).hasSize(0); assertThat(readGoods.getWorkflowEntity().getOperations()).hasSize(0);
} }
} }
@Test
void setArrayListDoInsertInsteadUpdate() {
var attachment1 = new Attachment();
attachment1.setName("File1");
var attachment2 = new Attachment();
attachment2.setName("File2");
var goods = new GoodsEntity();
goods.setName("goods1");
goods.setAttachments(List.of(attachment1, attachment2));
DB.save(goods);
String goodAsJson = DB.json().toJson(goods);
var marshaledGoods = DB.json().toBean(GoodsEntity.class, goodAsJson);
var attachment3a = new Attachment();
attachment3a.setName("File3");
marshaledGoods.getAttachments().add(attachment3a);
var persistedGoods = DB.find(GoodsEntity.class, goods.getId());
// this was not deleting orphans and so the inserts throw exception due primary key conflict
persistedGoods.setAttachments(marshaledGoods.getAttachments());
LoggedSql.start();
try {
logger.info("Saving goods with set new attachments list");
DB.save(persistedGoods);
} catch (Exception e) {
logger.error("Insert instead update", e);
}
var sql = LoggedSql.collect();
assertThat(sql.get(0)).contains("delete from attachment where goods_entity_id = ?");
persistedGoods = DB.find(GoodsEntity.class, goods.getId());
// this works good using .clear() and .addAll()
// because here we are mutating the collection, it detects the
// orphan removals occurring via the .clear()
marshaledGoods = DB.json().toBean(GoodsEntity.class, goodAsJson);
var attachment3b = new Attachment();
attachment3b.setName("File3");
marshaledGoods.getAttachments().add(attachment3b);
persistedGoods.getAttachments().clear();
persistedGoods.getAttachments().addAll(marshaledGoods.getAttachments());
LoggedSql.collect();
try {
logger.info("Saving goods with clear/added attachments");
DB.save(persistedGoods);
} catch (Exception e) {
logger.error("Insert instead update", e);
}
sql = LoggedSql.stop();
assertThat(sql.get(0)).contains("delete from attachment where id=?");
var persistedGoods2 = DB.find(GoodsEntity.class, goods.getId());
assertThat(persistedGoods2.getAttachments()).hasSize(3);
}
} }
@@ -0,0 +1,16 @@
package org.tests.o2m.dm;
import javax.persistence.Entity;
@Entity
public class Attachment extends HistoryColumns {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -1,8 +1,7 @@
package org.tests.o2m.dm; package org.tests.o2m.dm;
import javax.persistence.CascadeType; import javax.persistence.*;
import javax.persistence.Entity; import java.util.List;
import javax.persistence.OneToOne;
@Entity @Entity
public class GoodsEntity extends HistoryColumns { public class GoodsEntity extends HistoryColumns {
@@ -11,6 +10,9 @@ public class GoodsEntity extends HistoryColumns {
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
private WorkflowEntity workflowEntity; private WorkflowEntity workflowEntity;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Attachment> attachments;
public String getName() { public String getName() {
return name; return name;
} }
@@ -26,4 +28,12 @@ public class GoodsEntity extends HistoryColumns {
public void setWorkflowEntity(WorkflowEntity workflowEntity) { public void setWorkflowEntity(WorkflowEntity workflowEntity) {
this.workflowEntity = workflowEntity; this.workflowEntity = workflowEntity;
} }
public List<Attachment> getAttachments() {
return attachments;
}
public void setAttachments(List<Attachment> attachments) {
this.attachments = attachments;
}
} }