Skip to content

Commit

Permalink
DATAJPA-177 - Throw exception if entity to delete does not exist.
Browse files Browse the repository at this point in the history
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
odrotbohm committed Apr 15, 2012
1 parent 24e4ac8 commit 11d624f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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.