Reputation: 16656
I'm new to DynamoDB, I'm trying to insert a new item. However, I'm getting the following exception:
com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: XXX)
This is how my table is described:
{
"Table": {
"TableArn": "arn:aws:dynamodb:us-east-1:111:table/table-XXX",
"AttributeDefinitions": [
{
"AttributeName": "timestamp",
"AttributeType": "S"
},
{
"AttributeName": "title",
"AttributeType": "S"
}
],
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"WriteCapacityUnits": 5,
"ReadCapacityUnits": 5
},
"TableSizeBytes": 0,
"TableName": "ddb-table-sst67gy",
"TableStatus": "ACTIVE",
"KeySchema": [
{
"KeyType": "HASH",
"AttributeName": "title"
},
{
"KeyType": "RANGE",
"AttributeName": "timestamp"
}
],
"ItemCount": 0,
"CreationDateTime": 1489090172.658
}
}
And this is my Java class:
@DynamoDBTable(tableName = "table-XXX")
public class Movie {
private String title;
private String timeStamp;
@DynamoDBHashKey(attributeName = "title")
@NotNull(message = "Title must not be empty")
public String getTitle() {
return title;
}
public Movie withTitle(String name) {
setTitle(name);
return this;
}
@DynamoDBAttribute(attributeName = "timestamp")
public String getTimeStamp() {
return timeStamp;
}
public Movie withTimeStamp(String address) {
setTimeStamp(address);
return this;
}
public void setTitle(String title) {
this.title = title;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o;
if (title != null ? !title.equals(movie.title) : movie.title != null) return false;
return timeStamp != null ? timeStamp.equals(movie.timeStamp) : movie.timeStamp == null;
}
@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + (timeStamp != null ? timeStamp.hashCode() : 0);
return result;
}
}
How should I map my Java class into DynamoDB correctly ?
Upvotes: 1
Views: 727
Reputation: 39156
Please use annotation @DynamoDBRangeKey
to define the range or sort key attribute.
@DynamoDBRangeKey(attributeName = "timestamp")
public String getTimeStamp() {
return timeStamp;
}
Upvotes: 1