Skip to content

Commit 92b6b04

Browse files
committed
DATAJDBC-438 - Polishing.
Code style. Choose better matching exception.
1 parent dfaf0ac commit 92b6b04

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.Map;
2222

23-
import org.springframework.dao.TransientDataAccessResourceException;
23+
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
2424
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2525
import org.springframework.data.mapping.PersistentPropertyPath;
2626
import org.springframework.data.relational.core.conversion.DbAction;
@@ -52,6 +52,7 @@
5252
@RequiredArgsConstructor
5353
class DefaultJdbcInterpreter implements Interpreter {
5454

55+
public static final String UPDATE_FAILED = "Failed to update entity [%s]. Id [%s] not found in database.";
5556
private final RelationalMappingContext context;
5657
private final DataAccessStrategy accessStrategy;
5758

@@ -84,11 +85,11 @@ public <T> void interpret(InsertRoot<T> insert) {
8485
*/
8586
@Override
8687
public <T> void interpret(Update<T> update) {
87-
boolean updated = accessStrategy.update(update.getEntity(), update.getEntityType());
88-
if (!updated) {
89-
Object idValue = getIdFrom(update);
90-
throw new TransientDataAccessResourceException(String.format(
91-
"Failed to update entity [%s]. Id [%s] does not exist.", update.getEntityType(), idValue));
88+
89+
if (!accessStrategy.update(update.getEntity(), update.getEntityType())) {
90+
91+
throw new IncorrectUpdateSemanticsDataAccessException(
92+
String.format(UPDATE_FAILED, update.getEntity(), getIdFrom(update)));
9293
}
9394
}
9495

@@ -98,11 +99,11 @@ public <T> void interpret(Update<T> update) {
9899
*/
99100
@Override
100101
public <T> void interpret(UpdateRoot<T> update) {
101-
boolean updated = accessStrategy.update(update.getEntity(), update.getEntityType());
102-
if (!updated) {
103-
Object idValue = getIdFrom(update);
104-
throw new TransientDataAccessResourceException(String.format(
105-
"Failed to update root [%s]. Id [%s] does not exist.", update.getEntityType(), idValue));
102+
103+
if (!accessStrategy.update(update.getEntity(), update.getEntityType())) {
104+
105+
throw new IncorrectUpdateSemanticsDataAccessException(
106+
String.format(UPDATE_FAILED, update.getEntity(), getIdFrom(update)));
106107
}
107108
}
108109

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreterUnitTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import org.junit.Test;
2626
import org.mockito.ArgumentCaptor;
27-
27+
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
2828
import org.springframework.dao.TransientDataAccessResourceException;
2929
import org.springframework.data.annotation.Id;
3030
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
@@ -156,13 +156,18 @@ public void generateCascadingIds() {
156156
);
157157
}
158158

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

165-
interpreter.interpret(containerUpdate);
166+
assertThatExceptionOfType(IncorrectUpdateSemanticsDataAccessException.class).isThrownBy(() -> {
167+
interpreter.interpret(containerUpdate);
168+
}) //
169+
.withMessageContaining(Long.toString(CONTAINER_ID)) //
170+
.withMessageContaining(container.toString());
166171
}
167172

168173
@SuppressWarnings("unused")

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import lombok.Data;
2222
import lombok.EqualsAndHashCode;
2323

24-
import java.util.AbstractMap;
2524
import java.util.ArrayList;
2625
import java.util.Arrays;
2726
import java.util.Collections;
@@ -42,6 +41,7 @@
4241
import org.springframework.context.annotation.Bean;
4342
import org.springframework.context.annotation.Configuration;
4443
import org.springframework.context.annotation.Import;
44+
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
4545
import org.springframework.data.annotation.Id;
4646
import org.springframework.data.annotation.ReadOnlyProperty;
4747
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
@@ -205,12 +205,15 @@ public void updateReferencedEntityToNull() {
205205
softly.assertAll();
206206
}
207207

208-
@Test(expected = DbActionExecutionException.class) // DATAJDBC-438
208+
@Test // DATAJDBC-438
209209
public void updateFailedRootDoesNotExist() {
210+
210211
LegoSet entity = new LegoSet();
211-
entity.setId(100L); // not exist
212+
entity.setId(100L); // does not exist in the database
212213

213-
template.save(entity);
214+
assertThatExceptionOfType(DbActionExecutionException.class) //
215+
.isThrownBy(() -> template.save(entity)) //
216+
.withCauseInstanceOf(IncorrectUpdateSemanticsDataAccessException.class);
214217
}
215218

216219
@Test // DATAJDBC-112
@@ -622,7 +625,8 @@ public void readOnlyGetsLoadedButNotWritten() {
622625
template.save(entity);
623626

624627
assertThat(
625-
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class)).isEqualTo("from-db");
628+
jdbcTemplate.queryForObject("SELECT read_only FROM with_read_only", Collections.emptyMap(), String.class))
629+
.isEqualTo("from-db");
626630
}
627631

628632
private static NoIdMapChain4 createNoIdMapTree() {
@@ -886,8 +890,7 @@ static class NoIdMapChain4 {
886890
static class WithReadOnly {
887891
@Id Long id;
888892
String name;
889-
@ReadOnlyProperty
890-
String readOnly;
893+
@ReadOnlyProperty String readOnly;
891894
}
892895

893896
@Configuration

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/SimpleJdbcRepositoryEventsUnitTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public void before() {
8686

8787
this.dataAccessStrategy = spy(new DefaultDataAccessStrategy(generatorSource, context, converter, operations));
8888
delegatingDataAccessStrategy.setDelegate(dataAccessStrategy);
89-
9089
doReturn(true).when(dataAccessStrategy).update(any(), any());
9190

9291
JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter, publisher,

0 commit comments

Comments
 (0)