Migrate "DynamoDBAutoGeneratedKey" to DynamoDbEnhancedClient


I am using AWS version 2 and using DynamoDbEnhancedClient for dynamodb operations. I want to have a primary key which should be auto-generated. I see that in previous version of DynamoDB, we have the option of using DynamoDBAutoGeneratedKey annotation for this. But I am not able to find way to configure a auto-generated key with AWS version 2 (DynamoDbEnhancedClient )

Can somebody guide me please.



asked 2 years ago1044 views
2 Answers

This functionality is not yet available for Enhanced Client. I would suggest adding a feature request on the repo: https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced

profile pictureAWS
answered 2 years ago

To achieve this in DynamoDbEnhancedClient first we need to activate the extension while creating the DynamoDbEnhancedClient bean. Please find the attached code for your reference

Step 1 :-

public DynamoDbClient getDynamoDbClientForEC2() {
        return DynamoDbClient.builder()
 @ConditionalOnProperty(prefix = "aws", name = "access.secured", havingValue = "enabled")
    public DynamoDbEnhancedClient getDynamoDbEnhancedClientForEC2() {
        return DynamoDbEnhancedClient.builder()
                .extensions(AutoGeneratedTimestampRecordExtension.create(), AutoGeneratedUuidExtension.create())

Step 2 :- Finally, you can tell DynamoDbEnhancedClient to autogenerate the key for you in the entity class,

public class MyEntity {
private String myPartionKey;

    public String getMyPartionKey() {
        return myPartionKey;
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions