Cannot reuse aggregate identifier

When i send my first command i have the below error, yet the domainevents table is empty

Cannot reuse aggregate identifier [ExternalId(value=1e4b8fdb-25a4-4cd4-8afe-fe2d10ee95ea)] to create aggregate [PayerAggregate] since identifiers need to be unique.
commandGateway.send<ConfirmPayer>(
    ConfirmPayer(
        externalId = ExternalId(),
        rqUID = request.rqUID,
        phoneNumber = request.phoneNumber
    )
)

@Aggregate
internal class PayerAggregate() {

    @AggregateIdentifier
    private lateinit var externalId: ExternalId

    private val confirmPayerRq = ConfirmPayerRq()

    @CommandHandler
    @CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING)
    fun handle(command: ConfirmPayer) {
        AggregateLifecycle.apply(
            PayerConfirmed(
                externalId = command.externalId,
                rqUID = command.rqUID,
                phoneNumber = command.phoneNumber
            )
        )
    }
}

data class ConfirmPayer(
    @TargetAggregateIdentifier val externalId: ExternalId,
    val rqUID: String,
    val phoneNumber: String
    val timestamp: Instant = Instant.now()
)

data class ExternalId(val value: UUID = UUID.randomUUID()) {
    companion object {
        fun of(value: String) = ExternalId(UUID.fromString(value))
    }

    fun asReference() = value.toString()
}

Hi, could you share the entire stacktrace of the exception you’re getting? I think one of the causes is likely to explain more of what’s going on.

Which database are you using?

I think I saw this error before and it was happening while using H2 DB > 1.4 and hibernate, setting
spring.jpa.properties.hibernate.id.new_generator_mappings=true solved the problem

I using mongodb

	<dependency>
		<groupId>org.axonframework.extensions.mongo</groupId>
		<artifactId>axon-mongo</artifactId>
		<version>4.6.0</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-mongodb</artifactId>
	</dependency>

I don’t get a stacktrace for the exception, i get the message in swagger when i test the api.
I knew the cause of the problem but I don’t know how to solve it.

The problem in the line 39, when i tried to create an instance of XMLGregorianCalendar, when i change dueDate = newXMLGregorianCalendar() to dueDate = null it works fine. demo/Controller.kt at main · akanzari/demo · GitHub

@Steven_van_Beelen you have any idea please ?

It’s odd you’re not getting an exception, as the message you share is simply bound to a AggregateStreamCreationException, ConcurrencyException, or EventStoreException.
This is fixed in the AbstractEventStorageEngine implementation, which you’re definitely using when you have Mongo configured.

In all honesty, I don’t see how this would impact your Event Store’s remark that the Aggregate Identifier should be unique.
The RequestConfirmPayer uses the @TargetAggregateIdenifier annotated field, which is the ExternalId in your code, to set the aggregate identifier on the events to publish.

What might cause issues is the ExternalId itself. Axon Framework expects the aggregate identifiers to have a working toString() method. As it’s a data class, I would expect it to work. But perhaps enforcing this may solve something.
However, without a concrete stack trace, it’s hard to predict where the issue lies.

Might externalId be null? Or what happens, if you try it without lateinit? Have you tried to write a Aggregate Test and is there a similar or other problem?