Annotation Interface EventSourcedEntity
Annotation to configure several aspects of an event-sourced entity. This annotation is required to construct an
annotation-based entity through the
EventSourcedEntityModule.autodetected(Class, Class).
Event Criteria
While loading the entity from theEventSourcingRepository, the provided id needs to be translated to
an EventCriteria instance to load the correct events. Unless overridden, this translation is done by the
AnnotationBasedEventCriteriaResolver. The EventCriteria will be resolved in the following order:
-
Using the custom
criteriaResolverDefinition(). The definition should return aCriteriaResolver, implement theCriteriaResolverDefinitioninterface, and have a no-arg constructor. -
By defining a static method in the entity class annotated with
EventCriteriaBuilderwhich returns anEventCriteriaand accepts theidas a parameter. This method should be static and return anEventCriteria. Multiple methods can be defined with different id types, and the first matching method will be used. Optionally, you can defineMessageTypeResolveras a second parameter to resolve the type of the message. Other arguments are not supported. -
If no matching
EventCriteriaBuilderis found, thetagKey()will be used as the tag key, and theObject.toString()of the id will be used as value. -
If the
tagKey()is empty, theClass.getSimpleName()of the entity will be used as the key, and theObject.toString()of the id will be used as value.
Entity Factory
Event-sourced entities need to be instantiated before they can be evolved based on past events. TheEventSourcedEntityFactory is responsible for creating a new instance of the entity.
By default, this is done based on constructors or static methods annotated with EntityCreator.
These creators can take the payload or message of the first event, or the entity id as a parameter.
For more information, see the examples in the Javadoc of the annotation.
Command handling
Entities can declareCommandHandler-annotated methods
to execute a command on the entity. When a command targets an entity, the following steps are taken:
- The
EntityCommandHandlingComponentwill use theEntityIdResolverDefinitionto resolve the entity id from the command message. - The entity id is used to resolve the
EventCriteriafor the entity, as described above. - The
EventSourcedEntityFactoryis used to create a new instance of the entity by theEventSourcingRepository. - Existing events for the entity are used to
evolvethe entity. - The command is called on the entity in case of a
instance command handler, or on thecreational command handlerif it did not exist.
By default, the id is resolved using the AnnotationBasedEntityIdResolver, which resolves the
id based on the TargetEntityId annotation on a field or method
of the command payload. You can customize this behavior by providing a custom entityIdResolverDefinition().
Polymorphic entities
Polymorphic entities are entities that can have multiple concrete types, and the type of the entity is determined by the payload of the first event. In this case, theconcreteTypes() should be
specified with the concrete types of the entity.- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionClass<?>[]If the entity is a polymorphic entity, any subclasses that should be considered concrete types of the entity should be specified here.Class<? extends CriteriaResolverDefinition> The definition of theCriteriaResolverto use to resolve theEventCriteriafor the entity.Class<? extends EventSourcedEntityFactoryDefinition> The definition of theEventSourcedEntityFactoryto use to create a new instance of the entity.Class<? extends EntityIdResolverDefinition> The definition of theEntityIdResolverDefinitionto use to resolve the entity id from acommand message.The tag name to use when resolving theEventCriteriafor the entity.
-
Element Details
-
tagKey
String tagKeyThe tag name to use when resolving theEventCriteriafor the entity. If empty, the simple name of the entity class will be used.This value does not take effect if a matching
EventCriteriaBuilderis found, or a customcriteriaResolverDefinition()is provided.- Returns:
- The tag name to use when resolving the
EventCriteriafor the entity.
- Default:
""
-
concreteTypes
Class<?>[] concreteTypesIf the entity is a polymorphic entity, any subclasses that should be considered concrete types of the entity should be specified here. Classes that are not specified here will not be scanned.- Returns:
- The concrete types of the entity that should be considered when building the
AnnotatedEntityMetamodel.
- Default:
{}
-
criteriaResolverDefinition
Class<? extends CriteriaResolverDefinition> criteriaResolverDefinitionThe definition of theCriteriaResolverto use to resolve theEventCriteriafor the entity. A custom definition can be provided to override the default behavior of theAnnotationBasedEventCriteriaResolver.- Returns:
- The definition to construct a
CriteriaResolverDefinition.
- Default:
org.axonframework.eventsourcing.annotation.AnnotationBasedEventCriteriaResolverDefinition.class
-
entityFactoryDefinition
Class<? extends EventSourcedEntityFactoryDefinition> entityFactoryDefinitionThe definition of theEventSourcedEntityFactoryto use to create a new instance of the entity. A custom definition can be provided to override the default behavior of theAnnotationBasedEventSourcedEntityFactoryDefinition.- Returns:
- The definition to construct an
EventSourcedEntityFactory.
- Default:
org.axonframework.eventsourcing.annotation.reflection.AnnotationBasedEventSourcedEntityFactoryDefinition.class
-
entityIdResolverDefinition
Class<? extends EntityIdResolverDefinition> entityIdResolverDefinitionThe definition of theEntityIdResolverDefinitionto use to resolve the entity id from acommand message. Defaults to theAnnotatedEntityIdResolverDefinition, which resolves the entity id based on theTargetEntityIdannotation on a payload field or method, after converting the payload to the representation wanted by the entity.- Returns:
- The definition to construct an
EntityIdResolverDefinition.
- Default:
org.axonframework.modelling.entity.annotation.AnnotatedEntityIdResolverDefinition.class
-