Skip to content

Commit

Permalink
DATAJDBC-438 - Throw an exception if a save operation updates zero rows.
Browse files Browse the repository at this point in the history
Original pull request: #178.
  • Loading branch information
mhyeon-lee authored and schauder committed Nov 13, 2019
1 parent 93f0440 commit dfaf0ac
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.Map;

import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.conversion.DbAction;
Expand All @@ -46,6 +47,7 @@
*
* @author Jens Schauder
* @author Mark Paluch
* @author Myeonghyeon Lee
*/
@RequiredArgsConstructor
class DefaultJdbcInterpreter implements Interpreter {
Expand Down Expand Up @@ -82,7 +84,12 @@ public <T> void interpret(InsertRoot<T> insert) {
*/
@Override
public <T> void interpret(Update<T> update) {
accessStrategy.update(update.getEntity(), update.getEntityType());
boolean updated = accessStrategy.update(update.getEntity(), update.getEntityType());
if (!updated) {
Object idValue = getIdFrom(update);
throw new TransientDataAccessResourceException(String.format(
"Failed to update entity [%s]. Id [%s] does not exist.", update.getEntityType(), idValue));
}
}

/*
Expand All @@ -91,7 +98,12 @@ public <T> void interpret(Update<T> update) {
*/
@Override
public <T> void interpret(UpdateRoot<T> update) {
accessStrategy.update(update.getEntity(), update.getEntityType());
boolean updated = accessStrategy.update(update.getEntity(), update.getEntityType());
if (!updated) {
Object idValue = getIdFrom(update);
throw new TransientDataAccessResourceException(String.format(
"Failed to update root [%s]. Id [%s] does not exist.", update.getEntityType(), idValue));
}
}

/*
Expand Down
Expand Up @@ -25,12 +25,14 @@
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.conversion.DbAction.Insert;
import org.springframework.data.relational.core.conversion.DbAction.InsertRoot;
import org.springframework.data.relational.core.conversion.DbAction.UpdateRoot;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.domain.Identifier;
Expand All @@ -40,6 +42,7 @@
*
* @author Jens Schauder
* @author Mark Paluch
* @author Myeonghyeon Lee
*/
public class DefaultJdbcInterpreterUnitTests {

Expand Down Expand Up @@ -153,6 +156,15 @@ public void generateCascadingIds() {
);
}

@Test(expected = TransientDataAccessResourceException.class) // DATAJDBC-438
public void throwExceptionUpdateFailedRootDoesNotExist() {
container.id = CONTAINER_ID;
UpdateRoot<Container> containerUpdate = new UpdateRoot<>(container);
when(dataAccessStrategy.update(container, Container.class)).thenReturn(false);

interpreter.interpret(containerUpdate);
}

@SuppressWarnings("unused")
static class Container {

Expand Down
Expand Up @@ -48,6 +48,7 @@
import org.springframework.data.jdbc.testing.DatabaseProfileValueSource;
import org.springframework.data.jdbc.testing.HsqlDbOnly;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
Expand All @@ -67,6 +68,7 @@
* @author Jens Schauder
* @author Thomas Lang
* @author Mark Paluch
* @author Myeonghyeon Lee
*/
@ContextConfiguration
@Transactional
Expand Down Expand Up @@ -203,6 +205,14 @@ public void updateReferencedEntityToNull() {
softly.assertAll();
}

@Test(expected = DbActionExecutionException.class) // DATAJDBC-438
public void updateFailedRootDoesNotExist() {
LegoSet entity = new LegoSet();
entity.setId(100L); // not exist

template.save(entity);
}

@Test // DATAJDBC-112
public void replaceReferencedEntity() {

Expand Down
Expand Up @@ -65,6 +65,7 @@
* @author Jens Schauder
* @author Mark Paluch
* @author Oliver Gierke
* @author Myeonghyeon Lee
*/
public class SimpleJdbcRepositoryEventsUnitTests {

Expand All @@ -86,6 +87,8 @@ public void before() {
this.dataAccessStrategy = spy(new DefaultDataAccessStrategy(generatorSource, context, converter, operations));
delegatingDataAccessStrategy.setDelegate(dataAccessStrategy);

doReturn(true).when(dataAccessStrategy).update(any(), any());

JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter, publisher,
operations);

Expand Down

0 comments on commit dfaf0ac

Please sign in to comment.