Class AutoGeneratedUuidExtension

java.lang.Object
software.amazon.awssdk.enhanced.dynamodb.extensions.AutoGeneratedUuidExtension
All Implemented Interfaces:
DynamoDbEnhancedClientExtension

@SdkPublicApi @ThreadSafe public final class AutoGeneratedUuidExtension extends Object implements DynamoDbEnhancedClientExtension
This extension facilitates the automatic generation of a UUID value for tagged string attributes. UUID values are generated using UUID.randomUUID().

This extension is not loaded by default when you instantiate a DynamoDbEnhancedClient. Therefore, you need to specify it in a custom extension when creating the enhanced client.

Example to add AutoGeneratedUuidExtension along with default extensions is

 DynamoDbEnhancedClient.builder().extensions(Stream.concat(ExtensionResolver.defaultExtensions().stream(),
 Stream.of(AutoGeneratedUuidExtension.create())).collect(Collectors.toList())).build();

Example to just add AutoGeneratedUuidExtension without default extensions is

             DynamoDbEnhancedClient.builder().extensions(AutoGeneratedUuidExtension.create()).build();

To use auto-generated UUID values, create a field in your model that stores the UUID attribute. This field must be of type String, and you need to tag it as auto-generated UUID. If you are using the BeanTableSchema, then you should use the DynamoDbAutoGeneratedUuid annotation. If you are using the StaticTableSchema, then you should use the AutoGeneratedUuidExtension.AttributeTags.autoGeneratedUuidAttribute() static attribute tag.

Generation behavior is controlled by DynamoDbAutoGenerateStrategy:

  • ALWAYS: generate a new UUID on every write handled by this extension.
  • CREATE: generate a UUID only when the current value is missing. For values present in the write item map, missing means DynamoDB NULL; an empty string is treated as present.

For a non-primary-key CREATE attribute omitted from an updateItem write item map, this extension uses if_not_exists so DynamoDB applies the supplied UUID only when the stored attribute is absent. An existing stored value, including DynamoDB NULL or an empty string, is preserved. In contrast, an explicit DynamoDB NULL in the write item map is replaced with a generated UUID before the request is sent.

putItem remains a full replacement operation. Each putItem whose CREATE value is missing generates a UUID, including a subsequent put using the same primary key.

UpdateBehavior is a separate concern used while building update expressions for updateItem. It does not change this extension's strategy decision.