From cb2ca0de11911f31b93da7f0c555036fc6473d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bucht=C3=ADk?= Date: Mon, 30 Jan 2023 23:47:18 +0100 Subject: [PATCH 1/5] setArrayListDoInsertInsteadUpdate --- ...anyStatelessUpdateResultsInSoftDelete.java | 43 +++++++++++++++++-- .../java/org/tests/o2m/dm/Attachment.java | 16 +++++++ .../java/org/tests/o2m/dm/GoodsEntity.java | 16 +++++-- 3 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 ebean-test/src/test/java/org/tests/o2m/dm/Attachment.java diff --git a/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java b/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java index e6e86e5dd..d322ad3d3 100644 --- a/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java +++ b/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java @@ -5,12 +5,10 @@ import io.ebean.DB; import io.ebean.test.LoggedSql; import io.ebean.xtest.BaseTestCase; import org.junit.jupiter.api.Test; -import org.tests.o2m.dm.GoodsEntity; -import org.tests.o2m.dm.PersonEntity; -import org.tests.o2m.dm.WorkflowEntity; -import org.tests.o2m.dm.WorkflowOperationEntity; +import org.tests.o2m.dm.*; import java.io.StringWriter; +import java.sql.SQLException; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -235,4 +233,41 @@ class TestOneToManyStatelessUpdateResultsInSoftDelete extends BaseTestCase { assertThat(readGoods.getWorkflowEntity().getOperations()).hasSize(0); } } + + @Test + void setArrayListDoInsertInsteadUpdate() { + var attachment1 = new Attachment(); + attachment1.setName("File1"); + var attachment2 = new Attachment(); + attachment1.setName("File2"); + var goods = new GoodsEntity(); + goods.setName("goods1"); + goods.setAttachments(List.of(attachment1, attachment2)); + + DB.save(goods); + + var marshaledGoods = DB.json().toBean(GoodsEntity.class, DB.json().toJson(goods)); + var persistedGoods = DB.find(GoodsEntity.class, goods.getId()); + + // this forces insert and throws exception due primary key conflict + persistedGoods.setAttachments(marshaledGoods.getAttachments()); + try { + logger.info("Saving goods with set new attachments list"); + DB.save(persistedGoods); + } catch (Exception e) { + logger.error("Insert instead update", e); + } + + persistedGoods = DB.find(GoodsEntity.class, goods.getId()); + + // this works good + persistedGoods.getAttachments().clear(); + persistedGoods.getAttachments().addAll(marshaledGoods.getAttachments()); + try { + logger.info("Saving goods with clear/added attachments"); + DB.save(persistedGoods); + } catch (Exception e) { + logger.error("Insert instead update", e); + } + } } diff --git a/ebean-test/src/test/java/org/tests/o2m/dm/Attachment.java b/ebean-test/src/test/java/org/tests/o2m/dm/Attachment.java new file mode 100644 index 000000000..411695428 --- /dev/null +++ b/ebean-test/src/test/java/org/tests/o2m/dm/Attachment.java @@ -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; + } +} diff --git a/ebean-test/src/test/java/org/tests/o2m/dm/GoodsEntity.java b/ebean-test/src/test/java/org/tests/o2m/dm/GoodsEntity.java index ab29d5435..28482c805 100644 --- a/ebean-test/src/test/java/org/tests/o2m/dm/GoodsEntity.java +++ b/ebean-test/src/test/java/org/tests/o2m/dm/GoodsEntity.java @@ -1,8 +1,7 @@ package org.tests.o2m.dm; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.OneToOne; +import javax.persistence.*; +import java.util.List; @Entity public class GoodsEntity extends HistoryColumns { @@ -11,6 +10,9 @@ public class GoodsEntity extends HistoryColumns { @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true) private WorkflowEntity workflowEntity; + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) + private List attachments; + public String getName() { return name; } @@ -26,4 +28,12 @@ public class GoodsEntity extends HistoryColumns { public void setWorkflowEntity(WorkflowEntity workflowEntity) { this.workflowEntity = workflowEntity; } + + public List getAttachments() { + return attachments; + } + + public void setAttachments(List attachments) { + this.attachments = attachments; + } } From 39099bbcc9faea7a11fb891a614c054b31487100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bucht=C3=ADk?= Date: Tue, 31 Jan 2023 23:01:52 +0100 Subject: [PATCH 2/5] print sql --- ...ToManyStatelessUpdateResultsInSoftDelete.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java b/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java index d322ad3d3..ad8036b1f 100644 --- a/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java +++ b/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java @@ -239,14 +239,20 @@ class TestOneToManyStatelessUpdateResultsInSoftDelete extends BaseTestCase { var attachment1 = new Attachment(); attachment1.setName("File1"); var attachment2 = new Attachment(); - attachment1.setName("File2"); + attachment2.setName("File2"); + var attachment3 = new Attachment(); + attachment3.setName("File3"); var goods = new GoodsEntity(); goods.setName("goods1"); goods.setAttachments(List.of(attachment1, attachment2)); + LoggedSql.start(); + DB.save(goods); var marshaledGoods = DB.json().toBean(GoodsEntity.class, DB.json().toJson(goods)); + marshaledGoods.getAttachments().add(attachment3); + var persistedGoods = DB.find(GoodsEntity.class, goods.getId()); // this forces insert and throws exception due primary key conflict @@ -269,5 +275,13 @@ class TestOneToManyStatelessUpdateResultsInSoftDelete extends BaseTestCase { } catch (Exception e) { logger.error("Insert instead update", e); } + var persistedGoods2 = DB.find(GoodsEntity.class, goods.getId()); + assertThat(persistedGoods2.getAttachments()).hasSize(3); + + var sql = LoggedSql.collect(); + + LoggedSql.stop(); + + System.out.println(String.join("\n",sql)); } } From 244d45674a243fc578d4aa7f47689140ba8131c2 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 9 Feb 2023 23:29:13 +1300 Subject: [PATCH 3/5] #2952 #2953 - Fix for @OneToMany orphanRemoval not occurring orphanRemoval was not occurring when a loaded bean had a collection replaced by a new BeanCollection (as opposed to a vanilla collection like java.util.ArrayList). Json marshalling a collection puts beans into BeanCollection and this is part of the test that reproduced this issue. --- .../server/persist/SaveManyBeans.java | 11 ++++-- ...anyStatelessUpdateResultsInSoftDelete.java | 39 +++++++++++-------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java index 5b6858e78..7d0790d2d 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java @@ -37,6 +37,7 @@ final class SaveManyBeans extends SaveManyBase { private final boolean hasOrderColumn; private int sortOrder; private boolean insertAllChildren; + private boolean forceOrphanRemoval; SaveManyBeans(DefaultPersister persister, boolean insertedParent, BeanPropertyAssocMany many, EntityBean parentBean, PersistRequestBean request) { super(persister, insertedParent, many, parentBean, request); @@ -141,7 +142,7 @@ final class SaveManyBeans extends SaveManyBase { } private boolean forcedUpdateOrphanRemoval() { - return !insertedParent && many.isOrphanRemoval() && request.isForcedUpdate(); + return !insertedParent && many.isOrphanRemoval() && (forceOrphanRemoval || request.isForcedUpdate()); } private void saveAllBeans(final BeanProperty orderColumn) { @@ -228,8 +229,10 @@ final class SaveManyBeans extends SaveManyBase { if (detailBean instanceof EntityBean) { Object id = targetDescriptor.id(detailBean); if (!isNullOrZero(id)) { - // remember the Id (other details not in the collection) will be removed - detailIds.add(id); + if (!forceOrphanRemoval || !((EntityBean)detailBean)._ebean_getIntercept().isNew()) { + // remember the Id (other details not in the collection) will be removed + detailIds.add(id); + } } } } @@ -369,6 +372,8 @@ final class SaveManyBeans extends SaveManyBase { } } } + } else { + forceOrphanRemoval = !insertedParent && isChangedProperty(); } } } diff --git a/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java b/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java index ad8036b1f..b747cb904 100644 --- a/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java +++ b/ebean-test/src/test/java/org/tests/o2m/TestOneToManyStatelessUpdateResultsInSoftDelete.java @@ -8,7 +8,6 @@ import org.junit.jupiter.api.Test; import org.tests.o2m.dm.*; import java.io.StringWriter; -import java.sql.SQLException; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -240,48 +239,56 @@ class TestOneToManyStatelessUpdateResultsInSoftDelete extends BaseTestCase { attachment1.setName("File1"); var attachment2 = new Attachment(); attachment2.setName("File2"); - var attachment3 = new Attachment(); - attachment3.setName("File3"); + var goods = new GoodsEntity(); goods.setName("goods1"); goods.setAttachments(List.of(attachment1, attachment2)); - LoggedSql.start(); - DB.save(goods); - var marshaledGoods = DB.json().toBean(GoodsEntity.class, DB.json().toJson(goods)); - marshaledGoods.getAttachments().add(attachment3); + 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 forces insert and throws exception due primary key conflict + // 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 + // 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); + 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); - - var sql = LoggedSql.collect(); - - LoggedSql.stop(); - - System.out.println(String.join("\n",sql)); } } From 564194042b4a4a77d92c145ff2fd567bfc9b9366 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 9 Feb 2023 23:44:13 +1300 Subject: [PATCH 4/5] No effective change, BeanCollectionUtil perform the instanceof Collection before instanceof Map check --- .../ebeaninternal/server/deploy/BeanCollectionUtil.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanCollectionUtil.java b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanCollectionUtil.java index aa8c72b5a..b75e305e4 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanCollectionUtil.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/deploy/BeanCollectionUtil.java @@ -38,13 +38,10 @@ public final class BeanCollectionUtil { // returns a collection of beans return bc.getActualEntries(); } - - if (o instanceof Map) { - // yes, we want the entrySet (to set the keys) - return ((Map) o).entrySet(); - - } else if (o instanceof Collection) { + if (o instanceof Collection) { 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()); } From 0135263dbd13ad275deae68b3dddd0885e6e2278 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 9 Feb 2023 23:45:27 +1300 Subject: [PATCH 5/5] Style only for SaveManyBeans switching order in if/else --- .../java/io/ebeaninternal/server/persist/SaveManyBeans.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java index 7d0790d2d..44c9777b4 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/persist/SaveManyBeans.java @@ -362,7 +362,9 @@ final class SaveManyBeans extends SaveManyBase { if (!hasOrderColumn) { c.modifyReset(); } - if (modifyRemovals != null && !modifyRemovals.isEmpty()) { + if (modifyRemovals == null || modifyRemovals.isEmpty()) { + forceOrphanRemoval = !insertedParent && isChangedProperty(); + } else { for (Object removedBean : modifyRemovals) { if (removedBean instanceof EntityBean) { EntityBean eb = (EntityBean) removedBean; @@ -372,8 +374,6 @@ final class SaveManyBeans extends SaveManyBase { } } } - } else { - forceOrphanRemoval = !insertedParent && isChangedProperty(); } } }