I'm trying to store an array of strings within an AWS DynamoDB table. For the most part this array will be populated with at least one string. However there is the case where the array could be empty.
I've created a DynamoDB model in a Java Lambda function that has a set of strings as one of it's properties. If I try to save a DynamoDB model when the set of strings is empty it gives me an error saying I can't store an empty set in DynamoDB.
So my question is, how would handle removing that set property when it's empty from my model before I save / update it in the DynamoDB?
Here's an example of the model.
@DynamoDBTable(tableName = "group")
public class Group {
private String _id;
private Set<String> users;
@Null
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
public String getId() {
return _id;
}
public void setId(final String id) {
_id = id;
}
@DynamoDBAttribute
public Set<String> getUsers(){
return users;
}
public void setUsers(final Set<String> users) {
this.users = users;
}
public void addUser(String userId) {
if(this.users == null){
this.setUsers(new HashSet<String>(Arrays.asList(userId)));
}else{
this.getUsers().add(userId);
}
}
}
First time when I will create a group. It could have no user, or it could have one or more user.