Skip to content

Commit

Permalink
ISPN-10940 JPA deleteBatch should not pass empty collection to Criter…
Browse files Browse the repository at this point in the history
…iaDelete
  • Loading branch information
ryanemerson authored and pruivo committed Nov 14, 2019
1 parent 4c8c911 commit 49e74ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -562,6 +562,15 @@ public void testWriteAndDeleteBatch() throws Exception {
assertNull(cl.load("20"));
}

public void testEmptyWriteAndDeleteBatchIterable() {
assertIsEmpty();
assertNull("should not be present in the store", cl.load(0));
cl.writeBatch(Collections.emptyList());
assertEquals(0, cl.size());
cl.deleteBatch(Collections.emptyList());
assertEquals(0, cl.size());
}

public void testIsAvailable() {
assertTrue(cl.isAvailable());
}
Expand Down
Expand Up @@ -332,6 +332,12 @@ public void deleteBatch(Iterable<Object> keys) {
return;
}

// If the iterable is empty, then we can return before creating the EntityManager. Empty collections are not
// supported by all DB in CriteriaDelete
List<Object> keyCollection = StreamSupport.stream(keys.spliterator(), false).collect(Collectors.toList());
if (keyCollection.isEmpty())
return;

EntityManager em = emf.createEntityManager();
try {
EntityTransaction txn = em.getTransaction();
Expand All @@ -343,7 +349,7 @@ public void deleteBatch(Iterable<Object> keys) {
CriteriaDelete query = cb.createCriteriaDelete(configuration.entityClass());
Root root = query.from(configuration.entityClass());
SingularAttribute id = getEntityId(em, configuration.entityClass());
List<Object> keyCollection = StreamSupport.stream(keys.spliterator(), false).collect(Collectors.toList());

query.where(root.get(id).in(keyCollection));
em.createQuery(query).executeUpdate();

Expand Down

0 comments on commit 49e74ad

Please sign in to comment.