Annotation Interface InjectEntity


@Target(PARAMETER) @Retention(RUNTIME) public @interface InjectEntity
Annotation to be placed on a parameter of a MessageHandler annotated method that should receive an entity loaded from the StateManager.

The parameter should be one of the following three options:

  1. The exact entity type to inject, marked nullable if null can be expected.
  2. An Optional of the entity type to inject.
  3. A ManagedEntity of the entity identifier and entity type to inject.

The idProperty attribute can be used to specify the property of the message payload that contains the identifier of the entity to inject. If not specified, the idResolver is used to resolve the identifier of the entity to inject.

Unless a specific idResolver is specified, the AnnotationBasedEntityIdResolver is used to resolve the entity id from the message. This is based on finding a TargetEntityId annotation on a field or accessor method of the message payload.

So, identifiers will be resolved in the following order:

  1. From the property specified in idProperty.
  2. From the idResolver.
  3. From the TargetEntityId annotation on the message payload.

When the parameter is typed as the entity itself, and no entity can be found for the resolved identifier, the parameter's nullability determines the outcome. By default, an EntityNotFoundException is propagated, failing the message being handled. A parameter annotated with an annotation resembling "nullable" will instead resolve to null, allowing the handler to deal with a missing entity itself. This nullability support is in place to support a create-if-missing flow. A handler can check whether the injected entity is null, create the entity, and then proceed with subsequent tasks.

Declaring the parameter as Optional<MyEntity> achieves the same outcome without needing a "nullable" annotation: the parameter resolves to Optional.empty() instead of null. Kotlin nullability, as in MyEntity?, is supported as well.

A ManagedEntity-typed parameter is unaffected by nullability. Whether or not it is annotated "nullable", it is always passed through exactly as resolved by the StateManager backing this annotation. Note that the ManagedEntity.entity() is marked as nullable, thus supporting similar create-if-missing behavior as through the entity type or Optional path.

Since:
5.0.0
Author:
Mitchell Herrijgers
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The property of the message payload that contains the identifier of the entity to inject.
    The EntityIdResolver to resolve the identifier of the entity to inject.
  • Element Details

    • idProperty

      String idProperty
      The property of the message payload that contains the identifier of the entity to inject. If not specified, the idResolver is used to resolve the identifier of the entity to inject.
      Returns:
      The property of the message payload that contains the identifier of the entity to inject.
      Default:
      ""
    • idResolver

      Class<? extends EntityIdResolver> idResolver
      The EntityIdResolver to resolve the identifier of the entity to inject. Should have a no-arg constructor.
      Returns:
      The EntityIdResolver to resolve the identifier of the entity to inject.
      Default:
      org.axonframework.modelling.annotation.AnnotationBasedEntityIdResolver.class