Say, I want to save/create new item to the DynamoDb
table,
if and only if there is not any existent item already that that would contain the referenceId
equal to the same value I set.
In my case I want to create a item with withReferenceId=123
if there is not any other withReferenceId=123
in the table.
the referenceId
is not primary key! (I don not want it to be it)
So the code:
val withReferenceIdValue = "123";
val saveExpression = new DynamoDBSaveExpression();
final Map<String, ExpectedAttributeValue> expectedNoReferenceIdFound = new HashMap();
expectedNoReferenceIdFound.put(
"referenceId",
new ExpectedAttributeValue(new AttributeValue().withS(withReferenceIdValue)).withComparisonOperator(ComparisonOperator.NE)
);
saveExpression.setExpected(expectedNoReferenceIdFound);
newItemRecord.setReferenceId(withReferenceId);
this.mapper.save(newItemRecord, saveExpression); // do not fail..
That seems does not work.
I the table has the referenceId=123
already the save()
does not fail.
I expected this.mapper.save to fail with exception.
Q: How to make it fail on condition?
I also checked this one where they suggest to add auxiliary table (transaction-state table)..because seems the saveExpression works only for primary/partition key... if so:
not sure why there that limitation. in any case if it is primary key one can not create duplicated item with the same primary key.. why creating conditions on first place. 3rd table is too much.. why there is not just NE to whatever field I want to use. I may create an index for this filed. not being limited to use only primary key.. that what I mean
UPDATE:
My table mapping code:
@Data // I use [lombok][2] and it does generate getters and setters.
@DynamoDBTable(tableName = "MyTable")
public class MyTable {
@DynamoDBHashKey(attributeName = "myTableID")
@DynamoDBAutoGeneratedKey
private String myTableID;
@DynamoDBAttribute(attributeName = "referenceId")
private String referenceId;
@DynamoDBAttribute(attributeName = "startTime")
private String startTime;
@DynamoDBAttribute(attributeName = "endTime")
private String endTime;
...
}