Restore Aggregate from Snapshot

Hi All

I’ve configured Snapshots by providing this configuration:

@Bean
public SpringAggregateSnapshotter snapshotter(ParameterResolverFactory parameterResolverFactory,
                                              EventStore eventStore, TransactionManager transactionManager) {
    Executor executor = Executors.newSingleThreadExecutor();
    return new SpringAggregateSnapshotter(eventStore, parameterResolverFactory, executor, transactionManager);
}
@Bean
public Repository<Poll> pollRepository(Snapshotter snapshotter) {

    EventSourcingRepository<Poll> repository = new EventSourcingRepository<Poll>(
            pollAggregateFactory(),
            eventStore,
            new EventCountSnapshotTriggerDefinition(
                    snapshotter,
                    3)
    );
    return repository;
}

Now I constantly get this error 

Command 'wepoll.command.AttachTopicsCommand' resulted in org.axonframework.eventsourcing.IncompatibleAggregateException(Aggregate identifier must be non-null after applying an event. Make sure the aggregate identifier is initialized at the latest when handling the creation event.)

Does it mean that I need to provide some special functional to restore aggregate state from snapshot? Why I get this error? 
I'm new in Axon Framework... 



Hi Vladimir,

which Serializer do you use?

My hunch is that the serializer doesn’t deserialize the identifier field properly, leaving it null. In such case, Axon will complain, because that field must be set after applying the first event (which is the snapshot event in this case).

Cheers,

Allard

Hi Allard.

@Bean
public Serializer axonJsonSerializer() {
    return new JacksonSerializer();
}

I'm using JacksonSerializer

That actually explains it:

you probably have a getter to expose the identifier, but no setter to set it. Jackson requires getters and setters (or a constructor that allows values to be set). While that’s all very nice for commands and events, snapshots are a slightly different beast. Snapshots are in fact just serialized versions of the aggregate itself.
Since Axon 3.1, you can specify two different serializers: one for events, and one for the rest. Our recommendation would be to use JacksonSerializer for the Events and an XStreamSerializer for the rest.
In Spring Boot, you can configure your JacksonSerializer like this, to have Axon only use it for events, leaving the (default) XStreamSerializer for the other objects:

@Bean
@Qualifier(“eventSerializer”)
public Serializer eventSerializer() {
// create it and return it
}

Cheers,

Allard

With XStreamSerializer works pretty well so I’m leaving XSTream as is… It’s ok I think