最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon web services - How to retrieve generated UUID by @DynamoDbAutoGeneratedUuid (AWS DynamoDB Enhanced Client)? - Stack Overf

programmeradmin0浏览0评论

AWS DynamoDB Enhanced Client provides the AutoGeneratedUuidExtension and automatically generates a UUID during putItem operation. If using it as partition key, the newly created value needs to be retrieved otherwise a retrieval is not possible with getItem.

@DynamoDbBean
public class Example {

    private String uuid;

    @DynamoDbPartitionKey
    @DynamoDbAutoGeneratedUuid
    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}

The putItem method does not update the value of the bean:

@Test
void newOfferShouldHaveUuidGenerated() {
    Example example = new Example();
    table.putItem(example); // putItem is void
    assertThat(example.getUuid()).isNotNull();
}

AWS DynamoDB Enhanced Client provides the AutoGeneratedUuidExtension and automatically generates a UUID during putItem operation. If using it as partition key, the newly created value needs to be retrieved otherwise a retrieval is not possible with getItem.

@DynamoDbBean
public class Example {

    private String uuid;

    @DynamoDbPartitionKey
    @DynamoDbAutoGeneratedUuid
    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
}

The putItem method does not update the value of the bean:

@Test
void newOfferShouldHaveUuidGenerated() {
    Example example = new Example();
    table.putItem(example); // putItem is void
    assertThat(example.getUuid()).isNotNull();
}
Share Improve this question asked 2 days ago timomeinentimomeinen 3,3203 gold badges36 silver badges48 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

EnhancedClient uses a beforeWrite hook and does not bind to the object. My recommendation here is to auto generate your own UUID which you can manually set to your object instead of relying on the auto generated one.

Use DynamoDbTable::updateItem

Maybe not a good practice, but based on the documentation you can use updateItem instead of putItem:

Updates an item in the mapped table, or adds it if it doesn't exist. This operation calls the low-level DynamoDB API UpdateItem operation.

Example response = table.updateItem(example);

This return the generated uuid by the annotation @DynamoDbAutoGeneratedUuid, I already test it and it works.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论