Permalink
Browse files

DATAJPA-177 - Throw exception if entity to delete does not exist.

We now do an exists check inside SimpleJpaRepository.delete(ID id) before actually triggering the deletion to avoid an exception when trying to delete a null value. We throw an EmptyResultDataAccessException to indicate the nonexistent entity now.
  • Loading branch information...
1 parent 24e4ac8 commit 11d624f26609a5e03f305340b3a7e126cec158d8 @olivergierke olivergierke committed Apr 15, 2012
@@ -32,6 +32,7 @@
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
+import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
@@ -121,6 +122,12 @@ private String getCountQueryString() {
public void delete(ID id) {
Assert.notNull(id, "The given id must not be null!");
+
+ if (!exists(id)) {
+ throw new EmptyResultDataAccessException(String.format("No %s entity with id %s exists!",
+ entityInformation.getJavaType(), id), 1);
+ }
+
delete(findOne(id));
}
@@ -27,6 +27,7 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
+import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.sample.User;
@@ -81,4 +82,13 @@ public void doesNotActuallyRetrieveObjectsForPageableOutOfRange() {
verify(query, times(0)).getResultList();
}
+
+ /**
+ * @see DATAJPA-177
+ */
+ @Test(expected = EmptyResultDataAccessException.class)
+ public void throwsExceptionIfEntityToDeleteDoesNotExist() {
+
+ repo.delete(4711L);
+ }
}

0 comments on commit 11d624f

Please sign in to comment.