Testing whether an Aggregate with a given ID exists

I try to test whether an aggregate exists using the Repository, however the load() method throws an AggregateNotFoundException if I try to load a non-existing aggregate.

Is there a better way to test whether an Aggregate exists? One that doesn’t throws exceptions?

I am using axon 4.0.

I couldn’t find an issue tracker for Axon? Isn’t there any?

Kind regards,
David Stibbe

Hi David,

we have an issue tracker on GitHub: https://github.com/AxonFramework/AxonFramework/issues/

Strictly speaking, if you’re interested in knowing whether something exists, it would be a query. However, there are cases where you’d want to verify the presence of a certain aggregate. We have created an issue for that: https://github.com/AxonFramework/AxonFramework/issues/653
One this we plan to use this for, is create-or-update semantics for command handlers.



Hi Allard,

I will keep an eye on the issuetracker, thanks :slight_smile:

In the mean time I have cobbled together a distpatch interceptor for a specific single command, which checks whether the repository load method throws an exception on the specified Aggregate or not. Based on that it will either produce the

intercepted command (if the aggregate exists), or a new create command (based on the intercepted command), if it doesn’t.

Hi David,

I have the same problem and tried to avoid a dispatch interceptor by puting the commandGateway.sendAndWait for my Update-Command into a try-catch. The exceptions is caught and a CreateCommand is send instead. So far, so good. But the event handling afterwards is not behaving as expected: After processing a series of events, only one Event is beeing stored in the EventStore which is a JDBCEventStore. An external EventHandler-Class only gets one of the Events dispatched, so my read model ends up with only one item too… Bit strange.
I’m using Axon 4.1.1. btw.

Would you mind to share your solution?

Here is my code:

UpdateItemOptionBySapStockFileCommand updateCommand = new UpdateItemOptionBySapStockFileCommand(
        null, sapStockFileEntry.getTimestamp());
try {
} catch (AggregateNotFoundException e) {
    CreateItemOptionBySapStockFileCommand createCommand = new CreateItemOptionBySapStockFileCommand(sapStockFileEntry.getItemOptionId().toString(),
            null, sapStockFileEntry.getTimestamp());

Kind regards,
Achim Mahnke