Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to Dynamo table works fine, but reading throws DynamoDBMappingException

Executing orderRequestDao.save(new OrderRequest("5000", "body")); successfully places a record in Dynamo. Any attempts to read returns:

[Request processing failed; nested exception is com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException:
could not invoke null on class 
com.cfa.fulfillmentApi.model.OrderRequest 
with value 100 of type class java.lang.String] with root cause

(Record with id: 100 exists)

I'm using the following jars (aws.sdk.version: 1.11.86):

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>${aws.sdk.version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-core</artifactId>
        <version>${aws.sdk.version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-dynamodb</artifactId>
        <version>${aws.sdk.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.derjust</groupId>
        <artifactId>spring-data-dynamodb</artifactId>
        <version>4.4.1</version>
    </dependency>

DyamoDb config:

Primary partition key: id (String)

Dao:

@EnableScan
public interface OrderRequestDao extends CrudRepository<OrderRequest, String> {
    OrderRequest findOne(String s);
    OrderRequest save(OrderRequest or);
}

Domain object:

@DynamoDBTable(tableName = "dev_transx")
public class OrderRequest {

    private String id;
    private String body;

    public OrderRequest(String id, String body) {
        this.id = id;
        this.body = body;
    }

    public OrderRequest() {}

    @DynamoDBHashKey
    public String getId()
    {
        return id;
    }

    @DynamoDBAttribute
    public String getBody()
    {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, body='%s']",
                id, body);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

}

I've tried just about every data type for id in the domain class, but no luck.

like image 864
Joe Essey Avatar asked Dec 18 '22 09:12

Joe Essey


2 Answers

I removed aws-java-sdk-dynamodb since it was already in spring-data-dynamodb

Most importantly I added a setter for ID in the domain class.

like image 173
Joe Essey Avatar answered May 23 '23 08:05

Joe Essey


Adding a setter method for the field works like a charm

like image 24
Awadesh Avatar answered May 23 '23 09:05

Awadesh