Not sure if this issue is related to DynamoDB, Jackson, or something else.
The prefix is
is being removed from the field isPrimary
while writing, saving with the name primary
instead in DynamoDB.
Not able to get the record based on this field because:
- There's no column/field in dynamo DB as
isPrimary
- When trying to query with the field name
primary
,DynamoDbMapper
throws an exception saying primary is a reserved keyword and can not be used in the query.
Tryied using @JsonProperty(value = "isPrimary")
and @DynamoDBAttribute(attributeName = "isPrimary")
individually and both together, non of these work.
Using DynamoDBMapper
Entity Class
@Data
@Accessors(chain = true)
@DynamoDBTable(tableName = "my-data-table")
public class MyData {
@DynamoDBHashKey(attributeName = "id")
private String id;
@DynamoDBRangeKey(attributeName = "otherId")
private String otherId;
private String name;
@DynamoDBAttribute(attributeName = "isPrimary")
@JsonProperty(value = "isPrimary")
private boolean isPrimary;
@DynamoDBVersionAttribute
private Long version;
}
Read operation based on id
and isPrimary=true
public Optional<MyData> findPrimaryData(String id) {
Map<String, AttributeValue> entityAttributeMap = new HashMap<>();
entityAttributeMap.put(":id", new AttributeValue().withS(id));
entityAttributeMap.put(":isPrimary", new AttributeValue().withBOOL(true));
DynamoDBQueryExpression<MyData> queryExpressionExp = new DynamoDBQueryExpression<MyData>()
.withKeyConditionExpression("id = :id")
.withFilterExpression("isPrimary = :isPrimary")
.withExpressionAttributeValues(entityAttributeMap)
.withLimit(1);
List<MyData> list = dynamoDBMapper.query(MyData.class, queryExpressionExp);
if (list.isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(list.get(0));
}
Write operation to make isPrimary=true
for given id
and otherId
rest false
public void activate(String id, String otherId) {
Map<String, AttributeValue> entityAttributeMap = new HashMap<>();
entityAttributeMap.put(":id", new AttributeValue().withS(id));
DynamoDBQueryExpression<MyData> queryExpression = new DynamoDBQueryExpression<MyData>()
.withKeyConditionExpression("id = :id")
.withExpressionAttributeValues(entityAttributeMap);
List<MyData> list = dynamoDBMapper.query(MyData.class, queryExpression);
if (list.isEmpty()) {
throw new ResourceNotFoundException("MyData", "id", id);
}
list.stream()
.forEach(item ->
item.setPrimary(idPEntityItem.getOtherId().equals(otherId))
);
list.forEach(dynamoDBMapper::save);
}