Skip to content

Commit cac69a8

Browse files
committed
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
1 parent 93837a1 commit cac69a8

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,7 @@ private <T> EntityAndPreviousVersion<T> prepareVersionForUpdate(T instance) {
415415
// If the root aggregate has a version property, increment it.
416416
previousVersion = RelationalEntityVersionUtils.getVersionNumberFromEntity(instance, persistentEntity, converter);
417417

418-
Assert.notNull(previousVersion, "The root aggregate cannot be updated because the version property is null.");
419-
420-
long newVersion = previousVersion.longValue() + 1;
418+
long newVersion = (previousVersion == null ? 0 : previousVersion.longValue()) + 1;
421419

422420
preparedInstance = RelationalEntityVersionUtils.setVersionNumberOnEntity(instance, newVersion, persistentEntity,
423421
converter);

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

+30-7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.springframework.data.annotation.ReadOnlyProperty;
5555
import org.springframework.data.annotation.Version;
5656
import org.springframework.data.domain.PageRequest;
57+
import org.springframework.data.domain.Persistable;
5758
import org.springframework.data.domain.Sort;
5859
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
5960
import org.springframework.data.jdbc.core.convert.JdbcConverter;
@@ -278,7 +279,8 @@ void saveAndLoadManyEntitiesWithReferencedEntitySortedWithNullPrecedence() {
278279
template.save(createLegoSet("Star"));
279280
template.save(createLegoSet("Frozen"));
280281

281-
final Sort sort = Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST));
282+
final Sort sort =
283+
Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST));
282284
Iterable<LegoSet> reloadedLegoSets = template.findAll(LegoSet.class, sort);
283285

284286
assertThat(reloadedLegoSets) //
@@ -923,6 +925,16 @@ void saveAndUpdateAggregateWithPrimitiveShortVersion() {
923925
saveAndUpdateAggregateWithPrimitiveVersion(new AggregateWithPrimitiveShortVersion(), Number::shortValue);
924926
}
925927

928+
@Test // GH-1254
929+
void saveAndUpdateAggregateWithIdAndNullVersion() {
930+
931+
PersistableVersionedAggregate aggregate = new PersistableVersionedAggregate();
932+
aggregate.setVersion(null);
933+
aggregate.setId(23L);
934+
935+
assertThatThrownBy(() -> template.save(aggregate)).isInstanceOf(DbActionExecutionException.class);
936+
}
937+
926938
@Test // DATAJDBC-462
927939
@EnabledOnFeature(SUPPORTS_QUOTED_IDS)
928940
void resavingAnUnversionedEntity() {
@@ -1115,18 +1127,15 @@ static class ListParent {
11151127

11161128
@Column("id4") @Id private Long id;
11171129
String name;
1118-
@MappedCollection(idColumn = "LIST_PARENT")
1119-
List<ElementNoId> content = new ArrayList<>();
1130+
@MappedCollection(idColumn = "LIST_PARENT") List<ElementNoId> content = new ArrayList<>();
11201131
}
11211132

11221133
@Table("LIST_PARENT")
11231134
static class ListParentAllArgs {
11241135

1125-
@Column("id4") @Id
1126-
private final Long id;
1136+
@Column("id4") @Id private final Long id;
11271137
private final String name;
1128-
@MappedCollection(idColumn = "LIST_PARENT")
1129-
private final List<ElementNoId> content = new ArrayList<>();
1138+
@MappedCollection(idColumn = "LIST_PARENT") private final List<ElementNoId> content = new ArrayList<>();
11301139

11311140
@PersistenceConstructor
11321141
ListParentAllArgs(Long id, String name, List<ElementNoId> content) {
@@ -1287,6 +1296,20 @@ static abstract class VersionedAggregate {
12871296
abstract void setVersion(Number newVersion);
12881297
}
12891298

1299+
@Data
1300+
@Table("VERSIONED_AGGREGATE")
1301+
static class PersistableVersionedAggregate implements Persistable<Long> {
1302+
1303+
@Id private Long id;
1304+
1305+
@Version Long version;
1306+
1307+
@Override
1308+
public boolean isNew() {
1309+
return getId() == null;
1310+
}
1311+
}
1312+
12901313
@Value
12911314
@With
12921315
@Table("VERSIONED_AGGREGATE")

0 commit comments

Comments
 (0)