Conditional Update in DynamoDB Using Java (Guide w/ Code Examples)

Provided by Rafal Wilinski
Last updated: October 23rd, 2026
Add a ConditionExpression to UpdateItem to change an item only while its current state matches your expectation. A failed condition raises ConditionalCheckFailedException and leaves the item unchanged.
This example uses Java 17+ and the software.amazon.awssdk:dynamodb SDK 2.x dependency. Configure an AWS Region and credentials through your usual AWS profile or runtime role. The Orders table has String keys pk and sk; adapt the table and keys to your schema.
import java.util.*; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.*; public class Example { static AttributeValue s(String value) { return AttributeValue.builder().s(value).build(); } static Map<String, AttributeValue> key(String order) { return Map.of("pk", s("CUSTOMER#123"), "sk", s(order)); } public static void main(String[] args) throws InterruptedException { try (DynamoDbClient ddb = DynamoDbClient.create()) { UpdateItemResponse response = ddb.updateItem(UpdateItemRequest.builder() .tableName("Orders").key(key("ORDER#001")) .updateExpression("SET #status = :next") .conditionExpression("#status = :expected") .expressionAttributeNames(Map.of("#status", "status")) .expressionAttributeValues(Map.of(":next", s("SHIPPED"), ":expected", s("PENDING"))) .returnValues(ReturnValue.ALL_NEW).build()); System.out.println(response.attributes()); } } }
A condition failure is a business conflict, not a transient error to retry blindly. Reload the item and decide whether the transition is still valid.
Reference: AWS UpdateItem API. For related access patterns, see DynamoDB query examples.
Similar Code Examples
- Batch Update Using DynamoDB Mapper
- Remove Item in DynamoDB Using Java
- Batch Insert in DynamoDB Using Java
- Batch Delete Using DynamoDB Mapper
- Batch Save Using DynamoDB Mapper
- Delete by Hashkey Using DynamoDB Mapper
- Get All Items from DynamoDB Using Java
- Get Multiple Items in DynamoDB Using Java
- Remove with DynamoDB Mapper
- Update Multiple Attributes in DynamoDB Using Java
- Insert in DynamoDB Using Java
- Remove Attribute in DynamoDB Using Java
- Delete in DynamoDB Using Java
- Update Item in DynamoDB Using Java
- Get Item Request in DynamoDB Using Java
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today