我正在使用DynamoDBMapper將項目保存到沒有任何條件/表達式設置的DynamoDB中。但每次保存對象時都會收到ConditionalCheckFailed異常。參考下面的代碼。使用java將項目存儲到DynamoDB時發生ConditionalCheck發生異常
注意:我正在5線程下運行此代碼。
Main.java
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.regions.Region;
AmazonDynamoDBClient db = new AmazonDynamoDBClient();
Region region = Region.getRegion(regions);
db.setRegion(region);
DynamoDBMapperConfig config = new DynamoDBMapperConfig(DynamoDBMapperConfig.DEFAULT,
new DynamoDBMapperConfig(TableNameOverride.withTableNameReplacement("myTableName")));
DynamoDBMapper mapper = new DynamoDBMapper(db, config);
UserDocument userDocument = new UserDocument("UD001", "TestUser");
mapper.save(userDocument); # Getting ConditionalCheckFailed exception
UserDocument.java
public class UserDocument {
public UserDocument(String id, String account) {
this.id = id;
this.account = account;
}
@DynamoDBHashKey
@DynamoDBAttribute(attributeName = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@DynamoDBVersionAttribute
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
@DynamoDBAttribute(attributeName = "account")
public int getAccount() {
return account;
}
public void setAccount(int account) {
this.account = account;
}
}
您可以顯示TestClass中碼和值要分配到該對象? – notionquest
嗨,添加了我的測試類「UserDocument」和分配給它的值。 – Inaccessible