#357 - batch insertAll has no effect when tx commited

This commit is contained in:
Robin Bygrave
2015-07-30 04:23:02 +12:00
parent 19f40cb2e5
commit a5e94f6ae2
3 changed files with 72 additions and 3 deletions
@@ -24,6 +24,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Property mapped to a List Set or Map.
@@ -180,9 +181,27 @@ public class BeanPropertyAssocMany<T> extends BeanPropertyAssoc<T> {
help.add(bc, detailBean);
}
public boolean isEmptyBeanCollection(EntityBean bean) {
/**
* Return true if this is considered 'empty' from a save perspective.
*/
public boolean isEmptyBeanCollection(EntityBean bean, boolean insertedParent) {
Object val = getValue(bean);
return val == null || (val instanceof BeanCollection<?>) && ((BeanCollection<?>) val).isEmptyAndUntouched();
if (val == null) {
return true;
}
if ((val instanceof BeanCollection<?>)) {
return ((BeanCollection<?>) val).isEmptyAndUntouched();
}
if (insertedParent) {
// check 'vanilla' collection types
if (val instanceof Collection<?>){
return ((Collection<?>) val).isEmpty();
}
if (val instanceof Map<?,?>) {
return ((Map<?,?>) val).isEmpty();
}
}
return false;
}
/**
@@ -573,7 +573,7 @@ public final class DefaultPersister implements Persister {
BeanPropertyAssocMany<?>[] manys = desc.propertiesManySave();
for (int i = 0; i < manys.length; i++) {
// check that property is loaded and not empty uninitialised collection
if (request.isLoadedProperty(manys[i]) && !manys[i].isEmptyBeanCollection(parentBean)) {
if (request.isLoadedProperty(manys[i]) && !manys[i].isEmptyBeanCollection(parentBean, insertedParent)) {
saveMany(new SaveManyPropRequest(insertedParent, manys[i], parentBean, request), insertMode);
if (!insertedParent) {
request.addUpdatedManyProperty(manys[i]);
@@ -0,0 +1,50 @@
package com.avaje.tests.batchinsert;
import com.avaje.ebean.BaseTestCase;
import com.avaje.ebean.Ebean;
import com.avaje.ebean.Transaction;
import com.avaje.ebean.config.PersistBatch;
import com.avaje.tests.model.basic.OCachedBean;
import org.avaje.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestBatchInsertWithInitialisedCollection extends BaseTestCase {
@Test
public void test() {
List<OCachedBean> list = new ArrayList();
for (int i = 0; i < 3; i++) {
OCachedBean bean = new OCachedBean();
bean.setName("name "+i);
list.add(bean);
}
LoggedSqlCollector.start();
Transaction txn = Ebean.beginTransaction();
try {
txn.setBatch(PersistBatch.ALL);
Ebean.saveAll(list);
txn.commit();
} finally {
txn.end();
}
List<String> loggedSql = LoggedSqlCollector.stop();
assertThat(loggedSql).hasSize(3);
for (String sql : loggedSql) {
assertThat(sql).contains("insert into o_cached_bean (id, name) values (?,?);");
}
}
}