setArrayListDoInsertInsteadUpdate

This commit is contained in:
Michal Buchtík
2023-01-30 23:47:18 +01:00
parent 09a831f2d6
commit cb2ca0de11
3 changed files with 68 additions and 7 deletions
@@ -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);
}
}
}
@@ -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;
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<Attachment> 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<Attachment> getAttachments() {
return attachments;
}
public void setAttachments(List<Attachment> attachments) {
this.attachments = attachments;
}
}