Skip to content

Commit dad6e05

Browse files
committed
Avoid noop update for Id only aggregates.
Closes #1309
1 parent b46e342 commit dad6e05

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-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
@@ -127,7 +127,12 @@ public <T> Object[] insert(List<InsertSubject<T>> insertSubjects, Class<T> domai
127127

128128
@Override
129129
public <S> boolean update(S instance, Class<S> domainType) {
130-
return operations.update(sql(domainType).getUpdate(), sqlParametersFactory.forUpdate(instance, domainType)) != 0;
130+
131+
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forUpdate(instance, domainType);
132+
if (parameterSource.size() <= 1) {
133+
return true; // returning true, because conceptually the one row was correctly updated
134+
}
135+
return operations.update(sql(domainType).getUpdate(), parameterSource) != 0;
131136
}
132137

133138
@Override

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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,16 @@ void insertWithIdOnly() {
10211021
assertThat(template.save(entity).id).isNotNull();
10221022
}
10231023

1024+
@Test // GH-1309
1025+
void updateIdOnlyAggregate() {
1026+
1027+
WithIdOnly entity = new WithIdOnly();
1028+
1029+
assertThat(template.save(entity).id).isNotNull();
1030+
1031+
template.save(entity);
1032+
}
1033+
10241034
private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
10251035
Function<Number, T> toConcreteNumber) {
10261036
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
@@ -1456,6 +1466,7 @@ class WithIdOnly {
14561466
@Id Long id;
14571467
}
14581468

1469+
14591470
@Configuration
14601471
@Import(TestConfiguration.class)
14611472
static class Config {

0 commit comments

Comments
 (0)