Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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