Null Associated Value triggering in a Saga

We’re using an optional (nullable) parameter in an event and wish to only associate to a saga when it’s not null. Any advice in being able to do so without append null checks at the top of each SagaEventHandler?

I need to expand on my original question. On Friday I was iterating against an InMemorySagaStore(). Today I switched it to a JPA and began to see the following exceptions that result in the kafka consumer failing following the warning. Other side effects is that it fails to create the associate_values table as a result of not being able to handle the null value.

`

2019-10-21 09:08:28.256 WARN 13283 — [ricprocessor]-0] o.a.e.TrackingEventProcessor : Error occurred. Starting retry mode.

java.lang.NullPointerException: null
at org.axonframework.modelling.saga.repository.jpa.AssociationValueEntry.(AssociationValueEntry.java:68) ~[axon-modelling-4.1.2.jar:4.1.2]
at org.axonframework.modelling.saga.repository.jpa.JpaSagaStore.storeAssociationValue(JpaSagaStore.java:223) ~[axon-modelling-4.1.2.jar:4.1.2]
at org.axonframework.modelling.saga.repository.jpa.JpaSagaStore.insertSaga(JpaSagaStore.java:302) ~[axon-modelling-4.1.2.jar:4.1.2]
at org.axonframework.modelling.saga.repository.AnnotatedSagaRepository.storeSaga(AnnotatedSagaRepository.java:217) ~[axon-modelling-4.1.2.jar:4.1.2]
at org.axonframework.modelling.saga.repository.AnnotatedSagaRepository.lambda$doCreateInstance$3(AnnotatedSagaRepository.java:139) ~[axon-modelling-4.1.2.jar:4.1.2]

`

Hi Michael,

If you want to associate a value to a Saga instance conditionally, you are inclined to use the SagaLifecycle#associateWith(String, String) method.

The SagaEventHandler annotation does not give you the smarts to perform an if check on the optional; thus you’d definitely have to enter the event handling function first and then set the association yourself.

The exception you’re receiving occurs because an AssociationValueEntry just cannot have a null value field as that would mean there is no association at all.
The associations a Saga has with fields in events is key to making it work, so the option for that to be null is blocked out for you entirely.

So, sadly, you will have to do null checks.
To be fair, I would assume important association to not be null in an event regardless.
Granted, this might differ on the Domain you’re in (classical case of “it depends”), so do not view this comment as a remark to your design.

Hope this helps you out!

Cheers,
Steven

Thank you for the response. I think we’re going to proceed for now using an InMemorySagaStore. We’ll revisit this in the future and see if we can intercept associations to perform a null before writing to AssociationValueEntry. The primary issue with the current behavior is that it doesn’t simply log this as a warning/issue but prevents other events from processing.

I think the design could be revisited. Another way to solve this issue is to introduce more events. It was just much more convenient to piggyback on an existing one to trigger the Saga creation.