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 ac583991ac
commit a5238fac85
2 changed files with 34 additions and 12 deletions

View File

@@ -353,9 +353,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

@@ -53,6 +53,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;
@@ -258,14 +259,15 @@ class JdbcAggregateTemplateIntegrationTests {
}
@Test // GH-821
@EnabledOnFeature({SUPPORTS_QUOTED_IDS, SUPPORTS_NULL_PRECEDENCE})
@EnabledOnFeature({ SUPPORTS_QUOTED_IDS, SUPPORTS_NULL_PRECEDENCE })
void saveAndLoadManyEntitiesWithReferencedEntitySortedWithNullPrecedence() {
template.save(createLegoSet(null));
template.save(createLegoSet("Star"));
template.save(createLegoSet("Frozen"));
Iterable<LegoSet> reloadedLegoSets = template.findAll(LegoSet.class, Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST)));
Iterable<LegoSet> reloadedLegoSets = template.findAll(LegoSet.class,
Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST)));
assertThat(reloadedLegoSets) //
.extracting("name") //
@@ -843,7 +845,8 @@ class JdbcAggregateTemplateIntegrationTests {
assertThat(updatedRoot.version).isEqualTo(1L);
// Expect only one assignment of the version to AggregateWithImmutableVersion
assertThat(AggregateWithImmutableVersion.constructorInvocations).containsOnly(new ConstructorInvocation(savedRoot.id, updatedRoot.version));
assertThat(AggregateWithImmutableVersion.constructorInvocations)
.containsOnly(new ConstructorInvocation(savedRoot.id, updatedRoot.version));
}
@Test // DATAJDBC-219 Test that a delete with a version attribute works as expected.
@@ -908,6 +911,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() {
@@ -1075,18 +1088,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) {
@@ -1247,6 +1257,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")