0

I am using updateItem based on some condition. It works fine if condition is met, but it throws ConditionalCheckFailedException along with java InaccessibleObjectException if condition fails.

Why is it throwing InaccessibleObjectException? Also, how to handle it?

Update: Error also occurs in case of ValidationException

updateItemSpec = new UpdateItemSpec()
           .withConditionExpression()

table.update(updateItemSpec).getItem()

1 Answer 1

1

You are using an old Java API for Amazon DynamoDB. To update a table, consider moving away from V1 and use the Enhanced Client - which is part of the AWS SDK for Java V2. More information here:

Mapping items in DynamoDB tables

Here is the code to update a table using the Enhanced Client.

public class EnhancedModifyItem {

    public static void main(String[] args) {


        String usage = "Usage:\n" +
                "    UpdateItem <key> <email> \n\n" +
                "Where:\n" +
                "    key - the name of the key in the table (id120).\n" +
                "    email - the value of the modified email column.\n" ;

       if (args.length != 2) {
            System.out.println(usage);
            System.exit(1);
       }

        String key = args[0];
        String email = args[1];
        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder()
                .region(region)
                .build();

        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
                .dynamoDbClient(ddb)
                .build();

        String updatedValue = modifyItem(enhancedClient,key,email);
        System.out.println("The updated name value is "+updatedValue);
        ddb.close();
    }


    public static String modifyItem(DynamoDbEnhancedClient enhancedClient, String keyVal, String email) {
        try {
            //Create a DynamoDbTable object
            DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));

            //Create a KEY object
            Key key = Key.builder()
                    .partitionValue(keyVal)
                    .build();

            // Get the item by using the key and update the email value.
            Customer customerRec = mappedTable.getItem(r->r.key(key));
            customerRec.setEmail(email);
            mappedTable.updateItem(customerRec);
            return customerRec.getEmail();

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }
}

You can find all V2 DynamoDB examples here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.