Using two fields for the AggregateIdentifier

Hi,

I have this currently in my Aggregate:

@AggregateIdentifier
private String fooId;
private String versionNumber;

Is there a way in axon to create a composite aggregate identifier of these two fields?

Ideally, I guess UUID would be the best thing to use however the system was designed a while ago, and so at this point it can’t really be changed.

Alternatively, I could create a new field called “aggregateId” and do a concatenation of these two fields and set it as the AggregateIdentifier, but if there’s some sort of annotation which does it for me that would be great.

Thanks.

Hi Amasu,

@AggregateIdentifier can not be used on more than one field to combine them in a single aggregate identifier and there is currently no annotation to create composite identifiers.

However, as of Axon Framework 4.4, the @AggregateIdentifier annotation can also be put on methods. In your example, that annotated ID method can return the concatenated value of both fields, which would create a composite identifier.

Please note that @AggregateIdentifier-annotated methods must have no parameters.

For example:

@AggregateIdentifier
private String myAggregateIdentifier()

in this case the method name myAggregateIdentifier would be the routing key for your aggregate if you don’t specify one explicitly. This is important for Commands, as you would have to pay attention to field names of identifiers.

Alternatively, you can use a getter method like this:

@AggregateIdentifier
private String getIdentifier()

In this case, Axon Framework will honour the convention and the routing key will be just identifier, instead of the whole method name.

You are correct that UUID is one of the common ways to handle Aggregate identifiers in systems that can use them.

2 Likes

Hi @sandjelkovic, thanks for your response.

One more question however -

So in my Command POJO, I also need to implement this method and annotate it with @TargetAggregateIdentifier right?

Hi Amasu,

If you decide to have those fields split in the Command object instead of just one combined field, then you can use the same approach. Create and annotate a method of that Command with @TargetAggregateIdentifier that will combine those two fields.

However, you don’t have to use the same approach in Commands as in the Aggregates. You are free to use field in one and method in other, you just have to make sure the resulting names for the routing key are the same.