DefaultCrudRepository extends CrudRepositorySupport { public Object deleteById(Object entity) { final Object key = PropertyUtils.getPropertyValue(entity, getRepositoryMetadata().getIdentifierProperty()); if (key == null) { log.error("The entity that was supposed to be deleted, does not have a key"); throw new EntityMissingKeyException(getRepositoryMetadata().getEntityType(), getRepositoryMetadata().getIdentifierProperty()); } return delete(key); }  Iterable<Object> saveAll(Iterable entities); Optional<Object> findById(Object key); boolean existsById(Object key); Iterable findAllById(Iterable ids); Object delete(Object id); Object deleteById(Object entity); Iterable delete(Iterable entities); Iterable deleteAll();  }
@Test public void testDeleteByEntityWhenEntityHasKey() { final Person original = new Person(); dataStore.save("1", original); assertThat(dataStore.hasKey("1"), is(true)); final Object deleted = repository.deleteById(new Person().setId("1")); assertThat(dataStore.hasKey("1"), is(false)); assertThat(deleted, is(notNullValue())); assertThat(deleted, is(original)); }
@Test(expectedExceptions = EntityMissingKeyException.class) public void testDeleteByEntityWhenEntityHasNoKey() { repository.deleteById(new Person()); }