Reestablish previous exception behavior.

When saving an Aggregate which is not new, but has a null version attribute we now throw a DbActionExecutionException, like we used to.

Closes #1254
This commit is contained in:
Jens Schauder
2022-06-01 11:35:32 +02:00
parent 93837a198f
commit cac69a8a90
2 changed files with 31 additions and 10 deletions

View File

@@ -415,9 +415,7 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
// If the root aggregate has a version property, increment it.
previousVersion = RelationalEntityVersionUtils.getVersionNumberFromEntity(instance, persistentEntity, converter);
Assert.notNull(previousVersion, "The root aggregate cannot be updated because the version property is null.");
long newVersion = previousVersion.longValue() + 1;
long newVersion = (previousVersion == null ? 0 : previousVersion.longValue()) + 1;
preparedInstance = RelationalEntityVersionUtils.setVersionNumberOnEntity(instance, newVersion, persistentEntity,
converter);

View File

@@ -54,6 +54,7 @@ import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Persistable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
@@ -278,7 +279,8 @@ class JdbcAggregateTemplateIntegrationTests {
template.save(createLegoSet("Star"));
template.save(createLegoSet("Frozen"));
final Sort sort = Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST));
final Sort sort =
Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST));
Iterable<LegoSet> reloadedLegoSets = template.findAll(LegoSet.class, sort);
assertThat(reloadedLegoSets) //
@@ -923,6 +925,16 @@ class JdbcAggregateTemplateIntegrationTests {
saveAndUpdateAggregateWithPrimitiveVersion(new AggregateWithPrimitiveShortVersion(), Number::shortValue);
}
@Test // GH-1254
void saveAndUpdateAggregateWithIdAndNullVersion() {
PersistableVersionedAggregate aggregate = new PersistableVersionedAggregate();
aggregate.setVersion(null);
aggregate.setId(23L);
assertThatThrownBy(() -> template.save(aggregate)).isInstanceOf(DbActionExecutionException.class);
}
@Test // DATAJDBC-462
@EnabledOnFeature(SUPPORTS_QUOTED_IDS)
void resavingAnUnversionedEntity() {
@@ -1115,18 +1127,15 @@ class JdbcAggregateTemplateIntegrationTests {
@Column("id4") @Id private Long id;
String name;
@MappedCollection(idColumn = "LIST_PARENT")
List<ElementNoId> content = new ArrayList<>();
@MappedCollection(idColumn = "LIST_PARENT") List<ElementNoId> content = new ArrayList<>();
}
@Table("LIST_PARENT")
static class ListParentAllArgs {
@Column("id4") @Id
private final Long id;
@Column("id4") @Id private final Long id;
private final String name;
@MappedCollection(idColumn = "LIST_PARENT")
private final List<ElementNoId> content = new ArrayList<>();
@MappedCollection(idColumn = "LIST_PARENT") private final List<ElementNoId> content = new ArrayList<>();
@PersistenceConstructor
ListParentAllArgs(Long id, String name, List<ElementNoId> content) {
@@ -1287,6 +1296,20 @@ class JdbcAggregateTemplateIntegrationTests {
abstract void setVersion(Number newVersion);
}
@Data
@Table("VERSIONED_AGGREGATE")
static class PersistableVersionedAggregate implements Persistable<Long> {
@Id private Long id;
@Version Long version;
@Override
public boolean isNew() {
return getId() == null;
}
}
@Value
@With
@Table("VERSIONED_AGGREGATE")