Skip to content

IllegalArgumentException: The root aggregate cannot be updated because the version property is null #1254

Closed
@hantsy

Description

@hantsy

The complete example codes is here, https://github.com/hantsy/spring6-sandbox/blob/master/data-jdbc

There is a entity class implements Persistable and use the isNew to identify if it is a new entity.

@Table("persistable_posts")
@Setter
@Getter
@ToString
public class PersistablePost implements Persistable<UUID> {

    @Id
    UUID id;
    String title;
    String content;
    @Version
    Long version;

    @Override
    public boolean isNew() {
        return getId() == null;
    }
}

When I set a id and isNew returns true, thus calling template.save will try to update the entity that is not existed, and throw a DbActionExecutionException.

    @Test
    public void givenEntityImplementsPersistableInterface_whenIsNewReturnTrue_andSave_thenExecuteUpdateActionAndThrowsException() {
        var data = new PersistablePost();
        data.setTitle("test");
        data.setContent("test content");
        data.setId(UUID.randomUUID());
        // the id is set, then `isNew` returns a `true`, `save` method will execute a `update` action.
        assertThatThrownBy(() -> this.template.save(data)).isInstanceOf(DbActionExecutionException.class);
    }

These codes work well in the last milestone, when upgrading the latest milestone aligned with Spring 6.0.0-M4. The test failed.

java.lang.IllegalArgumentException: The root aggregate cannot be updated because the version property is null.
	at org.springframework.util.Assert.notNull(Assert.java:201)
	at org.springframework.data.jdbc.core.JdbcAggregateTemplate.prepareVersionForUpdate(JdbcAggregateTemplate.java:356)
	at org.springframework.data.jdbc.core.JdbcAggregateTemplate.lambda$save$1(JdbcAggregateTemplate.java:146)
	...(78 remaining lines not displayed - this can be changed with Assertions.setMaxStackTraceElementsDisplayed)

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions