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
2 Answers
Reset to default 1EnhancedClient 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.