Same @AggregateIdentifier for multiple Aggregates

Is it possible to have same aggregate Id for multiple aggregate classes? In the JPA code I can see that aggregate is loaded by ID only and not ID+Class. So if two aggregates has ids 1,2,3… I have no idea what will be loaded?

This is the query - there is no "WHERE e.payloadType=“java.xx.ClassName”

"SELECT new org.axonframework.eventhandling.GenericDomainEventEntry(" +
        "e.type, e.aggregateIdentifier, e.sequenceNumber, e.eventIdentifier, e.timeStamp, "
        + "e.payloadType, e.payloadRevision, e.payload, e.metaData) FROM "
        + domainEventEntryEntityName() + " e WHERE e.aggregateIdentifier = :id "
        + "AND e.sequenceNumber >= :seq ORDER BY e.sequenceNumber ASC"

Hi Bojan,

Nope, it is not possible to reuse a specific Aggregate Identifier for a different Aggregate Type.

This is enforced by the unique key constraint on the Aggregate Identifier - Sequence Number pair on the Event Store.
Thus, if you’d have Aggregate A store it’s first event, with id ‘some-id’ and sequence number ‘0’, then Aggregate B will not be allowed to publish an event with the same ‘some-id’ and sequence number ‘0’.

We typically suggest to use a UUID as the Aggregate Identifier, virtually ensuring you that there will be no collisions at all.
If for some reason your set up requires the reuse of Aggregate Identifiers between Aggregates, there are two things I can think off:

  1. Add the reused aggregate identifier per reference, but introduce am @AggregateIdentifier annotated UUID field along side it as the actual aggregate id from Axon’s perspective.
  2. Axon will always call the toString() method on the @AggregateIdentifier annotated field as the means to store the Event. Thus, if you’re using a Typed Id, you can change the toString() method to add the Aggregate Type yourself. That way, Axon will internally have a unique Aggregate Identifier, whilst from a usage perspective you can reuse the actual id.
    Hope this sheds some light on the situation Bojan!