Browse Source

Avoid noop update for Id only aggregates.

Closes #1309
pull/1319/head
Jens Schauder 3 years ago
parent
commit
dad6e056e8
No known key found for this signature in database
GPG Key ID: 45CC872F17423DBF
  1. 7
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java
  2. 4
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlIdentifierParameterSource.java
  3. 11
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java

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

@ -127,7 +127,12 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy { @@ -127,7 +127,12 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
@Override
public <S> boolean update(S instance, Class<S> domainType) {
return operations.update(sql(domainType).getUpdate(), sqlParametersFactory.forUpdate(instance, domainType)) != 0;
SqlIdentifierParameterSource parameterSource = sqlParametersFactory.forUpdate(instance, domainType);
if (parameterSource.size() <= 1) {
return true; // returning true, because conceptually the one row was correctly updated
}
return operations.update(sql(domainType).getUpdate(), parameterSource) != 0;
}
@Override

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

@ -81,4 +81,8 @@ class SqlIdentifierParameterSource extends AbstractSqlParameterSource { @@ -81,4 +81,8 @@ class SqlIdentifierParameterSource extends AbstractSqlParameterSource {
addValue(identifier, others.getValue(name), others.getSqlType(name));
}
}
int size() {
return namesToValues.size();
}
}

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

@ -1021,6 +1021,16 @@ class JdbcAggregateTemplateIntegrationTests { @@ -1021,6 +1021,16 @@ class JdbcAggregateTemplateIntegrationTests {
assertThat(template.save(entity).id).isNotNull();
}
@Test // GH-1309
void updateIdOnlyAggregate() {
WithIdOnly entity = new WithIdOnly();
assertThat(template.save(entity).id).isNotNull();
template.save(entity);
}
private <T extends Number> void saveAndUpdateAggregateWithVersion(VersionedAggregate aggregate,
Function<Number, T> toConcreteNumber) {
saveAndUpdateAggregateWithVersion(aggregate, toConcreteNumber, 0);
@ -1456,6 +1466,7 @@ class JdbcAggregateTemplateIntegrationTests { @@ -1456,6 +1466,7 @@ class JdbcAggregateTemplateIntegrationTests {
@Id Long id;
}
@Configuration
@Import(TestConfiguration.class)
static class Config {

Loading…
Cancel
Save