From 89bed8be670824f76b3ffecccd272392ab08d49a Mon Sep 17 00:00:00 2001
From: Robin Bygrave
Date: Mon, 20 Jul 2015 09:42:05 +1200
Subject: [PATCH] #326 - API Addition - saveAll(), deleteAll() etc
---
.gitignore | 1 +
src/main/java/com/avaje/ebean/Ebean.java | 58 +++++++--
.../java/com/avaje/ebean/EbeanServer.java | 123 +++++++++++++++---
.../server/core/DefaultServer.java | 101 +++++++++++---
.../ebeaninternal/api/TDSpiEbeanServer.java | 45 +++++++
.../tests/insert/TestInsertCollection.java | 15 ++-
6 files changed, 296 insertions(+), 47 deletions(-)
diff --git a/.gitignore b/.gitignore
index 65e36c5ce..6cbadb2b9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
*.autofetch
*.sql
+*.orig
.classpath
.project
.settings/
diff --git a/src/main/java/com/avaje/ebean/Ebean.java b/src/main/java/com/avaje/ebean/Ebean.java
index 17d7f2d05..5df6a1026 100644
--- a/src/main/java/com/avaje/ebean/Ebean.java
+++ b/src/main/java/com/avaje/ebean/Ebean.java
@@ -559,10 +559,20 @@ public final class Ebean {
}
/**
+ * Deprecated - please migrate to insertAll().
+ *
* Insert a collection of beans.
*/
+ @Deprecated
public static void insert(Collection> beans) {
- serverMgr.getDefaultServer().insert(beans);
+ serverMgr.getDefaultServer().insertAll(beans);
+ }
+
+ /**
+ * Insert a collection of beans.
+ */
+ public static void insertAll(Collection> beans) {
+ serverMgr.getDefaultServer().insertAll(beans);
}
/**
@@ -630,24 +640,46 @@ public final class Ebean {
}
/**
+ * Deprecate - please migrate to updateAll().
+ *
* Update the beans in the collection.
*/
+ @Deprecated
public static void update(Collection> beans) throws OptimisticLockException {
- serverMgr.getDefaultServer().update(beans);
+ serverMgr.getDefaultServer().updateAll(beans);
}
/**
+ * Update the beans in the collection.
+ */
+ public static void updateAll(Collection> beans) throws OptimisticLockException {
+ serverMgr.getDefaultServer().updateAll(beans);
+ }
+
+ /**
+ * Deprecated - please change to iterate yourself and save().
+ *
* Save all the beans from an Iterator.
*/
+ @Deprecated
public static int save(Iterator> iterator) throws OptimisticLockException {
return serverMgr.getDefaultServer().save(iterator);
}
/**
+ * Deprecated - please migrate to saveAll().
+ *
* Save all the beans from a Collection.
*/
public static int save(Collection> beans) throws OptimisticLockException {
- return serverMgr.getDefaultServer().save(beans);
+ return serverMgr.getDefaultServer().saveAll(beans);
+ }
+
+ /**
+ * Save all the beans from a Collection.
+ */
+ public static int saveAll(Collection> beans) throws OptimisticLockException {
+ return serverMgr.getDefaultServer().saveAll(beans);
}
/**
@@ -721,10 +753,13 @@ public final class Ebean {
}
/**
+ * Deprecated - please migrate to deleteAll().
+ *
* Delete several beans given their type and id values.
*/
+ @Deprecated
public static void delete(Class> beanType, Collection> ids) {
- serverMgr.getDefaultServer().delete(beanType, ids);
+ serverMgr.getDefaultServer().deleteAll(beanType, ids);
}
/**
@@ -737,8 +772,16 @@ public final class Ebean {
/**
* Delete all the beans from a Collection.
*/
- public static int delete(Collection> c) throws OptimisticLockException {
- return delete(c.iterator());
+ @Deprecated
+ public static int delete(Collection> beans) throws OptimisticLockException {
+ return serverMgr.getDefaultServer().deleteAll(beans);
+ }
+
+ /**
+ * Delete all the beans in the Collection.
+ */
+ public static int deleteAll(Collection> beans) throws OptimisticLockException {
+ return serverMgr.getDefaultServer().deleteAll(beans);
}
/**
@@ -1441,8 +1484,7 @@ public final class Ebean {
* @param deletes
* true if rows on the table where deleted
*/
- public static void externalModification(String tableName, boolean inserts, boolean updates,
- boolean deletes) {
+ public static void externalModification(String tableName, boolean inserts, boolean updates, boolean deletes) {
serverMgr.getDefaultServer().externalModification(tableName, inserts, updates, deletes);
}
diff --git a/src/main/java/com/avaje/ebean/EbeanServer.java b/src/main/java/com/avaje/ebean/EbeanServer.java
index 300e2f60a..bfc865434 100644
--- a/src/main/java/com/avaje/ebean/EbeanServer.java
+++ b/src/main/java/com/avaje/ebean/EbeanServer.java
@@ -1223,14 +1223,25 @@ public interface EbeanServer {
void save(Object bean) throws OptimisticLockException;
/**
+ * Deprecated - please change to iterate yourself and save.
+ *
* Save all the beans in the iterator.
*/
+ @Deprecated
int save(Iterator> it) throws OptimisticLockException;
+ /**
+ * Deprecated - please migrate to saveAll().
+ *
+ * Save all the beans in the collection.
+ */
+ @Deprecated
+ int save(Collection> beans) throws OptimisticLockException;
+
/**
* Save all the beans in the collection.
*/
- int save(Collection> beans) throws OptimisticLockException;
+ int saveAll(Collection> beans) throws OptimisticLockException;
/**
* Delete the bean.
@@ -1242,14 +1253,25 @@ public interface EbeanServer {
void delete(Object bean) throws OptimisticLockException;
/**
+ * Deprecated - please change to iterate yourself and delete.
+ *
* Delete all the beans from an Iterator.
*/
+ @Deprecated
int delete(Iterator> it) throws OptimisticLockException;
+ /**
+ * Deprecated - please migrate to deleteAll().
+ *
+ * Delete all the beans in the collection.
+ */
+ @Deprecated
+ int delete(Collection> beans) throws OptimisticLockException;
+
/**
* Delete all the beans in the collection.
*/
- int delete(Collection> c) throws OptimisticLockException;
+ int deleteAll(Collection> beans) throws OptimisticLockException;
/**
* Delete the bean given its type and id.
@@ -1262,15 +1284,45 @@ public interface EbeanServer {
int delete(Class> beanType, Object id, Transaction transaction);
/**
+ * Deprecated - please migrate to deleteAll().
+ *
* Delete several beans given their type and id values.
*/
+ @Deprecated
void delete(Class> beanType, Collection> ids);
+ /**
+ * Delete several beans given their type and id values.
+ */
+ void deleteAll(Class> beanType, Collection> ids);
+
+ /**
+ * Deprecated - please migrate to deleteAll().
+ *
+ * Delete several beans given their type and id values with an explicit
+ * transaction.
+ */
+ @Deprecated
+ void delete(Class> beanType, Collection> ids, Transaction transaction);
+
/**
* Delete several beans given their type and id values with an explicit
* transaction.
*/
- void delete(Class> beanType, Collection> ids, Transaction transaction);
+ void deleteAll(Class> beanType, Collection> ids, Transaction transaction);
+
+ /**
+ * Delete the bean with an explicit transaction.
+ */
+ void delete(Object bean, Transaction transaction) throws OptimisticLockException;
+
+ /**
+ * Deprecated - please migrate to iterate yourself and delete().
+ *
+ * Delete all the beans from an iterator.
+ */
+ @Deprecated
+ int delete(Iterator> it, Transaction transaction) throws OptimisticLockException;
/**
* Execute a Sql Update Delete or Insert statement. This returns the number of
@@ -1413,14 +1465,24 @@ public interface EbeanServer {
void save(Object bean, Transaction transaction) throws OptimisticLockException;
/**
+ * Deprecated - please change to iterate yourself and save.
* Save all the beans in the iterator with an explicit transaction.
*/
+ @Deprecated
int save(Iterator> it, Transaction transaction) throws OptimisticLockException;
+ /**
+ * Deprecated - please migrate to saveAll().
+ *
+ * Save all the beans in the collection with an explicit transaction.
+ */
+ @Deprecated
+ int save(Collection> beans, Transaction transaction) throws OptimisticLockException;
+
/**
* Save all the beans in the collection with an explicit transaction.
*/
- int save(Collection> beans, Transaction transaction) throws OptimisticLockException;
+ int saveAll(Collection> beans, Transaction transaction) throws OptimisticLockException;
/**
* Marks the entity bean as dirty.
@@ -1502,16 +1564,33 @@ public interface EbeanServer {
void update(Object bean, Transaction transaction, boolean deleteMissingChildren) throws OptimisticLockException;
/**
+ * Deprecated - please migrate to updateAll().
+ *
* Update a collection of beans. If there is no current transaction one is created and used to
* update all the beans in the collection.
*/
+ @Deprecated
void update(Collection> beans) throws OptimisticLockException;
+ /**
+ * Update a collection of beans. If there is no current transaction one is created and used to
+ * update all the beans in the collection.
+ */
+ void updateAll(Collection> beans) throws OptimisticLockException;
+
+ /**
+ * Deprecated - please migrate to updateAll().
+ *
+ * Update a collection of beans with an explicit transaction.
+ */
+ @Deprecated
+ void update(Collection> beans, Transaction transaction) throws OptimisticLockException;
+
/**
* Update a collection of beans with an explicit transaction.
*/
- void update(Collection> beans, Transaction transaction) throws OptimisticLockException;
-
+ void updateAll(Collection> beans, Transaction transaction) throws OptimisticLockException;
+
/**
* Insert the bean.
*
@@ -1528,15 +1607,32 @@ public interface EbeanServer {
void insert(Object bean, Transaction transaction);
/**
+ * Deprecated - please migrate to insertAll().
+ *
* Insert a collection of beans. If there is no current transaction one is created and used to
* insert all the beans in the collection.
*/
+ @Deprecated
void insert(Collection> beans);
+ /**
+ * Insert a collection of beans. If there is no current transaction one is created and used to
+ * insert all the beans in the collection.
+ */
+ void insertAll(Collection> beans);
+
+ /**
+ * Deprecated - please migrate to insertAll().
+ *
+ * Insert a collection of beans with an explicit transaction.
+ */
+ @Deprecated
+ void insert(Collection> beans, Transaction transaction);
+
/**
* Insert a collection of beans with an explicit transaction.
*/
- void insert(Collection> beans, Transaction transaction);
+ void insertAll(Collection> beans, Transaction transaction);
/**
* Delete the associations (from the intersection table) of a ManyToMany given
@@ -1545,7 +1641,7 @@ public interface EbeanServer {
* Typically these deletions occur automatically when persisting a ManyToMany
* collection and this provides a way to invoke those deletions directly.
*
- *
+ *
* @return the number of associations deleted (from the intersection table).
*/
int deleteManyToManyAssociations(Object ownerBean, String propertyName);
@@ -1560,7 +1656,7 @@ public interface EbeanServer {
* Typically these deletions occur automatically when persisting a ManyToMany
* collection and this provides a way to invoke those deletions directly.
*
- *
+ *
* @return the number of associations deleted (from the intersection table).
*/
int deleteManyToManyAssociations(Object ownerBean, String propertyName, Transaction transaction);
@@ -1624,15 +1720,6 @@ public interface EbeanServer {
*/
void saveAssociation(Object ownerBean, String propertyName, Transaction transaction);
- /**
- * Delete the bean with an explicit transaction.
- */
- void delete(Object bean, Transaction transaction) throws OptimisticLockException;
-
- /**
- * Delete all the beans from an iterator.
- */
- int delete(Iterator> it, Transaction transaction) throws OptimisticLockException;
/**
* Execute explicitly passing a transaction.
diff --git a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java
index 5f302bc94..d249a1285 100644
--- a/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java
+++ b/src/main/java/com/avaje/ebeaninternal/server/core/DefaultServer.java
@@ -38,6 +38,7 @@ import org.slf4j.LoggerFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
+import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@@ -1171,7 +1172,7 @@ public final class DefaultServer implements SpiEbeanServer {
BeanDescriptor desc = beanDescriptorManager.getBeanDescriptor(q.getBeanType());
- T bean = desc.cacheNaturalKeyLookup(q, (SpiTransaction)t);
+ T bean = desc.cacheNaturalKeyLookup(q, (SpiTransaction) t);
if (bean != null) {
return bean;
}
@@ -1503,13 +1504,23 @@ public final class DefaultServer implements SpiEbeanServer {
* Update all beans in the collection.
*/
public void update(Collection> beans) {
- update(beans, null);
+ updateAll(beans, null);
}
-
+
+ @Override
+ public void updateAll(Collection> beans) throws OptimisticLockException {
+ updateAll(beans, null);
+ }
+
+ @Override
+ public void update(Collection> beans, Transaction transaction) throws OptimisticLockException {
+ updateAll(beans, transaction);
+ }
+
/**
* Update all beans in the collection with an explicit transaction.
*/
- public void update(Collection> beans, Transaction t) {
+ public void updateAll(Collection> beans, Transaction t) {
if (beans == null || beans.isEmpty()) {
// Nothing to update?
@@ -1547,14 +1558,24 @@ public final class DefaultServer implements SpiEbeanServer {
/**
* Insert all beans in the collection.
*/
- public void insert(Collection> beans) {
- insert(beans, null);
+ public void insertAll(Collection> beans) {
+ insertAll(beans, null);
}
-
+
+ @Override
+ public void insert(Collection> beans) {
+ insertAll(beans, null);
+ }
+
+ @Override
+ public void insert(Collection> beans, Transaction transaction) {
+ insertAll(beans, transaction);
+ }
+
/**
* Insert all beans in the collection with a transaction.
*/
- public void insert(Collection> beans, Transaction t) {
+ public void insertAll(Collection> beans, Transaction t) {
if (beans == null || beans.isEmpty()) {
// Nothing to insert?
@@ -1674,7 +1695,7 @@ public final class DefaultServer implements SpiEbeanServer {
* number of beans that where saved.
*/
public int save(Iterator> it) {
- return save(it, null);
+ return saveAllInternal(it, null);
}
/**
@@ -1682,7 +1703,7 @@ public final class DefaultServer implements SpiEbeanServer {
* number of beans that where saved.
*/
public int save(Collection> c) {
- return save(c.iterator(), null);
+ return saveAllInternal(c.iterator(), null);
}
/**
@@ -1690,13 +1711,28 @@ public final class DefaultServer implements SpiEbeanServer {
* number of beans that where saved.
*/
public int save(Collection> c, Transaction t) {
- return save(c.iterator(), t);
+ return saveAllInternal(c.iterator(), t);
}
-
+
+ @Override
+ public int save(Iterator> it, Transaction transaction) throws OptimisticLockException {
+ return saveAllInternal(it, transaction);
+ }
+
+ @Override
+ public int saveAll(Collection> beans, Transaction transaction) throws OptimisticLockException {
+ return saveAllInternal(beans.iterator(), transaction);
+ }
+
+ @Override
+ public int saveAll(Collection> beans) throws OptimisticLockException {
+ return saveAllInternal(beans.iterator(), null);
+ }
+
/**
* Save all beans in the iterator with an explicit transaction.
*/
- public int save(Iterator> it, Transaction t) {
+ public int saveAllInternal(Iterator> it, Transaction t) {
TransWrapper wrap = initTransIfRequired(t);
try {
@@ -1739,11 +1775,23 @@ public final class DefaultServer implements SpiEbeanServer {
}
}
+ @Override
public void delete(Class> beanType, Collection> ids) {
- delete(beanType, ids, null);
+ deleteAll(beanType, ids, null);
}
- public void delete(Class> beanType, Collection> ids, Transaction t) {
+ @Override
+ public void delete(Class> beanType, Collection> ids, Transaction transaction) {
+ deleteAll(beanType, ids, transaction);
+ }
+
+ @Override
+ public void deleteAll(Class> beanType, Collection> ids) {
+ deleteAll(beanType, ids, null);
+ }
+
+ @Override
+ public void deleteAll(Class> beanType, Collection> ids, Transaction t) {
TransWrapper wrap = initTransIfRequired(t);
try {
@@ -1775,21 +1823,36 @@ public final class DefaultServer implements SpiEbeanServer {
/**
* Delete all the beans in the iterator.
*/
+ @Override
public int delete(Iterator> it) {
- return delete(it, null);
+ return deleteAllInternal(it, null);
}
/**
* Delete all the beans in the collection.
*/
- public int delete(Collection> c) {
- return delete(c.iterator(), null);
+ @Override
+ public int delete(Collection> beans) {
+ return deleteAllInternal(beans.iterator(), null);
+ }
+
+ @Override
+ public int delete(Iterator> it, Transaction transaction) throws OptimisticLockException {
+ return deleteAllInternal(it, transaction);
+ }
+
+ /**
+ * Delete all the beans in the collection.
+ */
+ @Override
+ public int deleteAll(Collection> beans) {
+ return deleteAllInternal(beans.iterator(), null);
}
/**
* Delete all the beans in the iterator with an explicit transaction.
*/
- public int delete(Iterator> it, Transaction t) {
+ private int deleteAllInternal(Iterator> it, Transaction t) {
TransWrapper wrap = initTransIfRequired(t);
diff --git a/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java b/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java
index 1fabf523f..14f051cbc 100644
--- a/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java
+++ b/src/test/java/com/avaje/ebeaninternal/api/TDSpiEbeanServer.java
@@ -727,4 +727,49 @@ public class TDSpiEbeanServer implements SpiEbeanServer {
public JsonContext json() {
return null;
}
+
+ @Override
+ public int saveAll(Collection> beans) throws OptimisticLockException {
+ return 0;
+ }
+
+ @Override
+ public int deleteAll(Collection> beans) throws OptimisticLockException {
+ return 0;
+ }
+
+ @Override
+ public void deleteAll(Class> beanType, Collection> ids) {
+
+ }
+
+ @Override
+ public void deleteAll(Class> beanType, Collection> ids, Transaction transaction) {
+
+ }
+
+ @Override
+ public int saveAll(Collection> beans, Transaction transaction) throws OptimisticLockException {
+ return 0;
+ }
+
+ @Override
+ public void updateAll(Collection> beans) throws OptimisticLockException {
+
+ }
+
+ @Override
+ public void updateAll(Collection> beans, Transaction transaction) throws OptimisticLockException {
+
+ }
+
+ @Override
+ public void insertAll(Collection> beans) {
+
+ }
+
+ @Override
+ public void insertAll(Collection> beans, Transaction transaction) {
+
+ }
}
diff --git a/src/test/java/com/avaje/tests/insert/TestInsertCollection.java b/src/test/java/com/avaje/tests/insert/TestInsertCollection.java
index 583f027d8..078ba233f 100644
--- a/src/test/java/com/avaje/tests/insert/TestInsertCollection.java
+++ b/src/test/java/com/avaje/tests/insert/TestInsertCollection.java
@@ -25,7 +25,7 @@ public class TestInsertCollection extends BaseTestCase {
customers.add(cust1);
customers.add(cust2);
- Ebean.insert(customers);
+ Ebean.insertAll(customers);
Assert.assertNotNull(cust1.getId());
Assert.assertNotNull(cust2.getId());
@@ -38,7 +38,7 @@ public class TestInsertCollection extends BaseTestCase {
cust1.setName("jim-changed");
cust2.setName("bob-changed");
- Ebean.update(customers);
+ Ebean.updateAll(customers);
Customer cust1Check2 = Ebean.find(Customer.class, cust1.getId());
Assert.assertEquals("jim-changed", cust1Check2.getName());
@@ -62,6 +62,17 @@ public class TestInsertCollection extends BaseTestCase {
Customer cust3Check = Ebean.find(Customer.class, cust3.getId());
Assert.assertEquals("mac", cust3Check.getName());
+ List deleteList = new ArrayList();
+ deleteList.add(cust1Check3);
+ deleteList.add(cust3);
+ deleteList.add(cust2Check2);
+
+ Ebean.deleteAll(deleteList);
+
+ Assert.assertNull(Ebean.find(Customer.class, cust1Check3.getId()));
+ Assert.assertNull(Ebean.find(Customer.class, cust2Check2.getId()));
+ Assert.assertNull(Ebean.find(Customer.class, cust3.getId()));
+
}