Skip to content

Commit d95b559

Browse files
committed
Avoid noop update for Id only aggregates.
Closes #1309
1 parent 345f12b commit d95b559

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domai
139139
*/
140140
@Override
141141
public <S> boolean update(S instance, Class<S> domainType) {
142-
return operations.update(sql(domainType).getUpdate(), sqlParametersFactory.forUpdate(instance, domainType)) != 0;
142+
143+
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forUpdate(instance, domainType);
144+
if (parameterSource.size() <= 1) {
145+
return true; // returning true, because conceptually the one row was correctly updated
146+
}
147+
return operations.update(sql(domainType).getUpdate(), parameterSource) != 0;
143148
}
144149

145150
/*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ void addAll(SqlIdentifierParameterSource others) {
8181
addValue(identifier, others.getValue(name), others.getSqlType(name));
8282
}
8383
}
84+
85+
int size() {
86+
return namesToValues.size();
87+
}
8488
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,16 @@ void insertWithIdOnly() {
983983
assertThat(template.save(entity).id).isNotNull();
984984
}
985985

986+
@Test // GH-1309
987+
void updateIdOnlyAggregate() {
988+
989+
WithIdOnly entity = new WithIdOnly();
990+
991+
assertThat(template.save(entity).id).isNotNull();
992+
993+
template.save(entity);
994+
}
995+
986996
@Test // GH-1232
987997
@EnabledOnFeature(IS_HSQL)
988998
void beforeSaveCallbackEffectsAreVisibleForInsert() {

0 commit comments

Comments
 (0)