mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1844 - Persisting an update of ElementCollection with JDBC batch, ensure delete, insert batch order
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.Version;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
public class EcsmChild {
|
||||
|
||||
@Id
|
||||
UUID oneId;
|
||||
|
||||
String name;
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "ecsm_values", joinColumns = @JoinColumn(name = "host_id", referencedColumnName = "one_id"))
|
||||
Set<String> values = new LinkedHashSet<>();
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public EcsmChild(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "id:" + oneId + " name:" + name + " values:" + values;
|
||||
}
|
||||
|
||||
public UUID getOneId() {
|
||||
return oneId;
|
||||
}
|
||||
|
||||
public void setOneId(UUID oneId) {
|
||||
this.oneId = oneId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public Set<String> getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
public void setValues(Set<String> values) {
|
||||
this.values = values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
import static javax.persistence.CascadeType.ALL;
|
||||
|
||||
@Entity
|
||||
public class EcsmParent {
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
|
||||
@Version
|
||||
private long version;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(cascade = ALL)
|
||||
private List<EcsmChild> children;
|
||||
|
||||
public EcsmParent(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<EcsmChild> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<EcsmChild> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,36 @@ public class TestElementCollectionBasic extends BaseTestCase {
|
||||
return EcPersonPersistAdapter.eventLog();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertThen_UpdateWhenNotChanged_expect_noChanges() {
|
||||
|
||||
EcPerson person = new EcPerson("Nothing021");
|
||||
person.getPhoneNumbers().add("021 1234");
|
||||
person.getPhoneNumbers().add("021 4321");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
Ebean.save(person);
|
||||
|
||||
List<String> sql = LoggedSqlCollector.current();
|
||||
assertThat(eventLog()).containsOnly("preInsert", "postInsert");
|
||||
assertThat(sql).hasSize(4);
|
||||
|
||||
final EcPerson found = Ebean.find(EcPerson.class, person.getId());
|
||||
found.getPhoneNumbers().size();
|
||||
|
||||
sql = LoggedSqlCollector.current();
|
||||
assertThat(sql).hasSize(2);
|
||||
assertThat(sql.get(0)).contains("from ec_person t0 where t0.id = ?");
|
||||
assertThat(sql.get(1)).contains("from ec_person_phone t0 where");
|
||||
|
||||
// save when not actually changed
|
||||
Ebean.save(found);
|
||||
|
||||
sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).isEmpty();
|
||||
assertThat(eventLog()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public class TestElementCollectionBasicMap extends BaseTestCase {
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
EcmPerson person = new EcmPerson("Fiona021");
|
||||
EcmPerson person = new EcmPerson("MFiona021");
|
||||
person.getPhoneNumbers().put("home", "021 1234");
|
||||
person.getPhoneNumbers().put("work", "021 4321");
|
||||
DB.save(person);
|
||||
@@ -36,7 +36,7 @@ public class TestElementCollectionBasicMap extends BaseTestCase {
|
||||
assertThat(sql.get(2)).contains("insert into ecm_person_phone");
|
||||
}
|
||||
|
||||
EcmPerson person1 = new EcmPerson("Fiona09");
|
||||
EcmPerson person1 = new EcmPerson("MFiona09");
|
||||
person1.getPhoneNumbers().put("home", "09 1234");
|
||||
person1.getPhoneNumbers().put("work", "09 4321");
|
||||
person1.getPhoneNumbers().put("mob", "09 9876");
|
||||
@@ -46,10 +46,12 @@ public class TestElementCollectionBasicMap extends BaseTestCase {
|
||||
|
||||
List<EcmPerson> found =
|
||||
DB.find(EcmPerson.class).where()
|
||||
.startsWith("name", "Fiona0")
|
||||
.startsWith("name", "MFiona0")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertThat(found).hasSize(2);
|
||||
|
||||
Map<String, String> phoneNumbers0 = found.get(0).getPhoneNumbers();
|
||||
Map<String, String> phoneNumbers1 = found.get(1).getPhoneNumbers();
|
||||
phoneNumbers0.size();
|
||||
@@ -68,7 +70,7 @@ public class TestElementCollectionBasicMap extends BaseTestCase {
|
||||
DB.find(EcmPerson.class)
|
||||
.fetch("phoneNumbers")
|
||||
.where()
|
||||
.startsWith("name", "Fiona0")
|
||||
.startsWith("name", "MFiona0")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestElementCollectionCascade extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EcsmParent parent = new EcsmParent("p1");
|
||||
final List<EcsmChild> children = parent.getChildren();
|
||||
|
||||
children.add(createChild("c0", 2));
|
||||
children.add(createChild("c1", 2));
|
||||
|
||||
DB.save(parent);
|
||||
|
||||
replaceWith(children.get(0), 22);
|
||||
replaceWith(children.get(1), 5);
|
||||
|
||||
parent.setName("p1-mod");
|
||||
DB.save(parent);
|
||||
|
||||
Map<String, Set<String>> childVals = new LinkedHashMap<>();
|
||||
|
||||
final EcsmParent foundParent = DB.find(EcsmParent.class, parent.getId());
|
||||
final List<EcsmChild> children1 = foundParent.getChildren();
|
||||
for (EcsmChild ecsmOne : children1) {
|
||||
final Set<String> values = ecsmOne.getValues();
|
||||
values.size();
|
||||
childVals.put(ecsmOne.getName(), values);
|
||||
}
|
||||
|
||||
Set<String> vals0 = childVals.get("c0");
|
||||
assertThat(vals0).hasSize(22);
|
||||
|
||||
Set<String> vals1 = childVals.get("c1");
|
||||
assertThat(vals1).hasSize(5);
|
||||
}
|
||||
|
||||
private void replaceWith(EcsmChild one, int count) {
|
||||
one.getValues().clear();
|
||||
createChild(one, count);
|
||||
}
|
||||
|
||||
private EcsmChild createChild(String name, int count) {
|
||||
return createChild(new EcsmChild(name), count);
|
||||
}
|
||||
|
||||
private EcsmChild createChild(EcsmChild one, int count) {
|
||||
final String name = one.getName();
|
||||
for (int i = 0; i < count; i++) {
|
||||
one.getValues().add(name + i);
|
||||
}
|
||||
return one;
|
||||
}
|
||||
}
|
||||
+4
-5
@@ -29,18 +29,17 @@ public class TestElementCollectionCascadeMultiple extends BaseTestCase {
|
||||
save(top);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(9);
|
||||
|
||||
assertThat(sql.get(0)).contains("insert into ecs_person");
|
||||
assertThat(sql.get(1)).contains("-- bind");
|
||||
assertThat(sql.get(2)).contains("insert into ecs_person_phone");
|
||||
assertThat(sql.get(2)).contains("insert into ec_top");
|
||||
assertThat(sql.get(3)).contains("-- bind");
|
||||
assertThat(sql.get(4)).contains("-- bind");
|
||||
assertThat(sql.get(5)).contains("insert into ec_top");
|
||||
assertThat(sql.get(4)).contains("insert into ecs_person_phone");
|
||||
assertThat(sql.get(5)).contains("-- bind");
|
||||
assertThat(sql.get(6)).contains("-- bind");
|
||||
assertThat(sql.get(7)).contains("insert into ec_top_ecs_person");
|
||||
assertThat(sql.get(8)).contains("-- bind");
|
||||
|
||||
assertThat(sql).hasSize(9);
|
||||
}
|
||||
|
||||
@Transactional(batchSize = 20)
|
||||
|
||||
Reference in New Issue
Block a user