DefaultJpaRepository extends CrudRepositorySupport { public Iterable deleteInBatch(Iterable entities) { final List<Object> keys = new LinkedList<>(); for (Object entity : entities) { final Object key = PropertyUtils.getPropertyValue(entity, getRepositoryMetadata().getIdentifierProperty()); if (key == null) { log.error("There is no key set for the entity we were trying to delete"); throw new EntityMissingKeyException(getRepositoryMetadata().getEntityType(), getRepositoryMetadata().getIdentifierProperty()); } keys.add(key); } return deleteByKeysInBatch(keys); }  void flush(); Iterable deleteInBatch(Iterable entities); Iterable deleteAllInBatch(); Object getOne(Object key); Object saveAndFlush(Object entity);  }
@Test public void testDeleteInBatch() { dataStore.save("1", new Person()); dataStore.save("2", new Person()); dataStore.save("3", new Person()); dataStore.save("4", new Person()); final List<String> existingIds = Arrays.asList("2", "3"); final List<String> missingIds = Arrays.asList("5", "6"); final List<Person> request = new ArrayList<>(); for (String id : existingIds) { request.add(new Person().setId(id)); } for (String id : missingIds) { request.add(new Person().setId(id)); } final List<Person> expected = new ArrayList<>(); for (String id : existingIds) { expected.add(dataStore.retrieve(id)); } for (String existingId : existingIds) { assertThat(dataStore.hasKey(existingId), is(true)); } final List<?> deleted = iterableToList(repository.deleteInBatch(request)); assertThat(deleted, hasSize(existingIds.size())); for (Object item : deleted) { assertThat(item, is(instanceOf(Person.class))); assertThat((Person) item, isIn(expected)); } }
@Test(expectedExceptions = EntityMissingKeyException.class) public void testDeleteInBatchWhenIdIsNull() { repository.deleteInBatch(Collections.singleton(new Person())); }