#367 - ENH: Add EbeanServer deleteAll() with an explicit transaction

This commit is contained in:
Robin Bygrave
2015-08-02 12:43:19 +12:00
parent 4327108ddf
commit fceeeacf88
4 changed files with 154 additions and 9 deletions
+12 -8
View File
@@ -1205,7 +1205,6 @@ public interface EbeanServer {
* public class Order { ...
*
* @OneToMany(cascade=CascadeType.ALL, mappedBy="order")
* @JoinColumn(name="order_id")
* List<OrderDetail> details;
* ...
* }
@@ -1235,9 +1234,9 @@ public interface EbeanServer {
void delete(Object bean) throws OptimisticLockException;
/**
* Delete all the beans in the collection.
* Delete the bean with an explicit transaction.
*/
int deleteAll(Collection<?> beans) throws OptimisticLockException;
void delete(Object bean, Transaction transaction) throws OptimisticLockException;
/**
* Delete the bean given its type and id.
@@ -1249,6 +1248,16 @@ public interface EbeanServer {
*/
int delete(Class<?> beanType, Object id, Transaction transaction);
/**
* Delete all the beans in the collection.
*/
int deleteAll(Collection<?> beans) throws OptimisticLockException;
/**
* Delete all the beans in the collection using an explicit transaction.
*/
int deleteAll(Collection<?> beans, Transaction transaction) throws OptimisticLockException;
/**
* Delete several beans given their type and id values.
*/
@@ -1260,11 +1269,6 @@ public interface EbeanServer {
*/
void deleteAll(Class<?> beanType, Collection<?> ids, Transaction transaction);
/**
* Delete the bean with an explicit transaction.
*/
void delete(Object bean, Transaction transaction) throws OptimisticLockException;
/**
* Execute a Sql Update Delete or Insert statement. This returns the number of
* rows that where updated, deleted or inserted. If is executed in batch then
@@ -1180,7 +1180,7 @@ public final class DefaultServer implements SpiEbeanServer {
BeanDescriptor<T> desc = beanDescriptorManager.getBeanDescriptor(query.getBeanType());
T bean = desc.cacheNaturalKeyLookup((SpiQuery<T>)query, (SpiTransaction) t);
T bean = desc.cacheNaturalKeyLookup((SpiQuery<T>) query, (SpiTransaction) t);
if (bean != null) {
return bean;
}
@@ -1779,6 +1779,14 @@ public final class DefaultServer implements SpiEbeanServer {
return deleteAllInternal(beans.iterator(), null);
}
/**
* Delete all the beans in the collection.
*/
@Override
public int deleteAll(Collection<?> beans, Transaction t) {
return deleteAllInternal(beans.iterator(), t);
}
/**
* Delete all the beans in the iterator with an explicit transaction.
*/
@@ -0,0 +1,128 @@
package com.avaje.ebean;
import com.avaje.tests.model.basic.EBasicVer;
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 EbeanServer_saveAllTest {
@Test
public void saveAll() {
List<EBasicVer> someBeans = beans(3);
// act
LoggedSqlCollector.start();
Ebean.saveAll(someBeans);
// assert
List<String> loggedSql = LoggedSqlCollector.stop();
for (String insertSql : loggedSql) {
assertThat(insertSql).contains("insert into e_basicver (id, name, description, other, last_update) values (");
}
for (EBasicVer someBean : someBeans) {
someBean.setName(someBean.getName()+"-mod");
}
// act
LoggedSqlCollector.start();
Ebean.updateAll(someBeans);
loggedSql = LoggedSqlCollector.stop();
for (String updateSql : loggedSql) {
assertThat(updateSql).contains("update e_basicver set name=?, last_update=? where id=? ");
}
// act
LoggedSqlCollector.start();
Ebean.deleteAll(someBeans);
loggedSql = LoggedSqlCollector.stop();
for (String updateSql : loggedSql) {
assertThat(updateSql).contains("delete from e_basicver where id=? ");
}
}
@Test
public void saveAll_withTransaction() {
List<EBasicVer> someBeans = beans(3);
EbeanServer server = Ebean.getDefaultServer();
// act
LoggedSqlCollector.start();
Transaction txn = server.beginTransaction();
try {
server.saveAll(someBeans, txn);
txn.commit();
} finally {
txn.end();
}
// assert
List<String> loggedSql = LoggedSqlCollector.stop();
for (String insertSql : loggedSql) {
assertThat(insertSql).contains("insert into e_basicver (id, name, description, other, last_update) values (");
}
for (EBasicVer someBean : someBeans) {
someBean.setName(someBean.getName() + "-mod");
}
// act
LoggedSqlCollector.start();
txn = server.beginTransaction();
try {
server.updateAll(someBeans, txn);
txn.commit();
} finally {
txn.end();
}
loggedSql = LoggedSqlCollector.stop();
for (String updateSql : loggedSql) {
assertThat(updateSql).contains("update e_basicver set name=?, last_update=? where id=? ");
}
// act
LoggedSqlCollector.start();
txn = server.beginTransaction();
try {
server.deleteAll(someBeans, txn);
txn.commit();
} finally {
txn.end();
}
loggedSql = LoggedSqlCollector.stop();
for (String updateSql : loggedSql) {
assertThat(updateSql).contains("delete from e_basicver where id=? ");
}
}
private List<EBasicVer> beans(int count) {
List<EBasicVer> beans = new ArrayList<EBasicVer>();
for (int i = 0; i <count; i++) {
beans.add(bean("foo"+i));
}
return beans;
}
private EBasicVer bean(String name) {
EBasicVer bean = new EBasicVer();
bean.setName(name);
return bean;
}
}
@@ -673,6 +673,11 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
return 0;
}
@Override
public int deleteAll(Collection<?> beans, Transaction transaction) throws OptimisticLockException {
return 0;
}
@Override
public void deleteAll(Class<?> beanType, Collection<?> ids) {