DynamoDBMapper仅在对象不存在时保存 [英] DynamoDBMapper save only if object doesn't exist

查看:97
本文介绍了DynamoDBMapper仅在对象不存在时保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java DynamoDBMapper,如何仅在对象不存在时(基于主键)保存对象。如果确实存在,我希望引发异常或失败,而不是更新现有条目。

Using the Java DynamoDBMapper, how can I save an object only if it doesn't already exist (based on primary key). If it does exist, I want an exception or failure to be thrown, rather than having the existing entry updated.

推荐答案

我相信您应该可以使用

I believe you should be able to do this with a DynamoDbSaveExpression object that can apply to the mapper.

AWS网站上有一个教程此处,代码如下:

There's a tutorial on the AWS site here, code shown below:

try {
    DynamoDBSaveExpression saveExpression = new       DynamoDBSaveExpression();
    Map expected = new HashMap();
    expected.put("status", new ExpectedAttributeValue().withExists(false));

    saveExpression.setExpected(expected);

    mapper.save(obj, saveExpression);
} catch (ConditionalCheckFailedException e) {
    // This means our save wasn't recorded, since our constraint wasn't met
    // If this happens, the worker can simply look for a new task to work on
}

这篇关于DynamoDBMapper仅在对象不存在时保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆