DynamoDB Update/Clobber issue
When user wants to update some specific attributes in an item, and user set those needed attributes values in the entity and created a config with save behavior as UPDATE. Expected behavior is AWS_SDK updates the provides values and leave the rest attribute values unchanged. But here, it deletes the value of other attributes which are not specified in the entity. Hope change is needed in method save() in DynamoDBMapper class.
else if (config.getSaveBehavior() != SaveBehavior.CLOBBER ) {
updateValues.put(attributeName, new AttributeValueUpdate().withAction("DELETE"));
}
This should be as
else if (config.getSaveBehavior() != SaveBehavior.UPDATE) {
updateValues.put(attributeName, new AttributeValueUpdate().withAction("DELETE"));
}
Hi tnatarajan,
This is actually an intended behavior, as described in javadoc for save operation:
For updates, a null value for an object property will remove it from that item in DynamoDB.
So for your case, one possible solution would be to use a trimmed domain class that doesn't model the attributes that are expected to remain unchanged.
Considering that this is rather an inconvenient workaround, yet it is still very important for us to avoid any changes that could break existing customers' code, we are thinking of providing a third option for SaveBehavior (something like UPDATE_IGNORE_NULL_ATTRIBUTES), which would force the save operation to NOT remove null-valued attributes.
By doing it, we could both support the desired "partial" UPDATE and keep our code as backward-compatible. What do you think?
Thanks. -Shuo
Hi Shuo, Thanks for clarification, that's good to know. It will be great if we have that new SaveBehavior.
Thanks, Thiru
Just letting you know that the latest version 1.5.3 includes a new SaveBehavior. From the release notes: The high-level DynamoDBMapper class now supports a new SaveBehavior strategy, called UPDATE_SKIP_NULL_ATTRIBUTES. This new SaveBehavior is very similar to the existing UPDATE strategy. The only difference is that any attributes sent with null values will not be removed from the item in DynamoDB.
Correction: UPDATE_SKIP_NULL_ATTRIBUTES is available in 1.5.4, not in 1.5.3: http://aws.amazon.com/releasenotes/Java/2365482772829983
Oops, sorry about that! Thanks @hanshuo-aws!