mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 14:32:04 +00:00
#1832 - Cascade insert @ElementCollection when marshalled from JSON
This commit is contained in:
@@ -21,22 +21,20 @@ class SaveManyElementCollection extends SaveManyBase {
|
||||
void save() {
|
||||
|
||||
Collection<?> collection = BeanCollectionUtil.getActualEntries(value);
|
||||
if (collection == null || !BeanCollectionUtil.isModified(value)) {
|
||||
return;
|
||||
}
|
||||
if (collection != null && (insertedParent || BeanCollectionUtil.isModified(value))) {
|
||||
Object parentId = request.getBeanId();
|
||||
preElementCollectionUpdate(parentId);
|
||||
|
||||
Object parentId = request.getBeanId();
|
||||
preElementCollectionUpdate(parentId);
|
||||
|
||||
transaction.depth(+1);
|
||||
SqlUpdate sqlInsert = server.createSqlUpdate(many.insertElementCollection());
|
||||
for (Object value : collection) {
|
||||
sqlInsert.setNextParameter(parentId);
|
||||
many.bindElementValue(sqlInsert, value);
|
||||
server.execute(sqlInsert, transaction);
|
||||
transaction.depth(+1);
|
||||
SqlUpdate sqlInsert = server.createSqlUpdate(many.insertElementCollection());
|
||||
for (Object value : collection) {
|
||||
sqlInsert.setNextParameter(parentId);
|
||||
many.bindElementValue(sqlInsert, value);
|
||||
server.execute(sqlInsert, transaction);
|
||||
}
|
||||
transaction.depth(-1);
|
||||
resetModifyState();
|
||||
postElementCollectionUpdate();
|
||||
}
|
||||
transaction.depth(-1);
|
||||
resetModifyState();
|
||||
postElementCollectionUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,24 +23,22 @@ class SaveManyElementCollectionMap extends SaveManyBase {
|
||||
void save() {
|
||||
|
||||
Set<Map.Entry<?, ?>> entries = (Set<Map.Entry<?, ?>>) BeanCollectionUtil.getActualEntries(value);
|
||||
if (entries == null || !BeanCollectionUtil.isModified(value)) {
|
||||
return;
|
||||
if (entries != null && (insertedParent || BeanCollectionUtil.isModified(value))) {
|
||||
Object parentId = request.getBeanId();
|
||||
preElementCollectionUpdate(parentId);
|
||||
|
||||
transaction.depth(+1);
|
||||
SqlUpdate sqlInsert = server.createSqlUpdate(many.insertElementCollection());
|
||||
for (Map.Entry<?, ?> entry : entries) {
|
||||
sqlInsert.setNextParameter(parentId);
|
||||
sqlInsert.setNextParameter(entry.getKey());
|
||||
many.bindElementValue(sqlInsert, entry.getValue());
|
||||
server.execute(sqlInsert, transaction);
|
||||
}
|
||||
|
||||
transaction.depth(-1);
|
||||
resetModifyState();
|
||||
postElementCollectionUpdate();
|
||||
}
|
||||
|
||||
Object parentId = request.getBeanId();
|
||||
preElementCollectionUpdate(parentId);
|
||||
|
||||
transaction.depth(+1);
|
||||
SqlUpdate sqlInsert = server.createSqlUpdate(many.insertElementCollection());
|
||||
for (Map.Entry<?, ?> entry : entries) {
|
||||
sqlInsert.setNextParameter(parentId);
|
||||
sqlInsert.setNextParameter(entry.getKey());
|
||||
many.bindElementValue(sqlInsert, entry.getValue());
|
||||
server.execute(sqlInsert, transaction);
|
||||
}
|
||||
|
||||
transaction.depth(-1);
|
||||
resetModifyState();
|
||||
postElementCollectionUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.text.json.JsonContext;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class EcmPersonTest {
|
||||
|
||||
@Test
|
||||
public void readJsonThenInsert() {
|
||||
|
||||
EcmPerson source = new EcmPerson("Ethan027");
|
||||
source.getPhoneNumbers().put("home", "027 1234");
|
||||
source.getPhoneNumbers().put("work", "027 4321");
|
||||
|
||||
final JsonContext json = DB.json();
|
||||
final String asJson = json.toJson(source);
|
||||
|
||||
final EcmPerson person = json.toBean(EcmPerson.class, asJson);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.save(person);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(4);
|
||||
assertThat(sql.get(0)).contains("insert into ecm_person ");
|
||||
assertThat(sql.get(1)).contains("insert into ecm_person_phone_numbers ");
|
||||
assertThat(sql.get(2)).contains("-- bind");
|
||||
assertThat(sql.get(3)).contains("-- bind");
|
||||
|
||||
DB.delete(person);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.text.json.JsonContext;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class EcsPersonTest {
|
||||
|
||||
@Test
|
||||
public void jsonReadThenInsert() {
|
||||
|
||||
EcsPerson source = new EcsPerson("Ethan027");
|
||||
source.getPhoneNumbers().add("027 1234");
|
||||
source.getPhoneNumbers().add("027 4321");
|
||||
|
||||
final JsonContext json = DB.json();
|
||||
final String asJson = json.toJson(source);
|
||||
|
||||
final EcsPerson person = json.toBean(EcsPerson.class, asJson);
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
DB.save(person);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql).hasSize(4);
|
||||
assertThat(sql.get(0)).contains("insert into ecs_person ");
|
||||
assertThat(sql.get(1)).contains("insert into ecs_person_phone ");
|
||||
assertThat(sql.get(2)).contains("-- bind");
|
||||
assertThat(sql.get(3)).contains("-- bind");
|
||||
|
||||
DB.delete(person);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user