Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load data from DynamoDB due to an attribute of type List<Object> which is not able to get unconverted and was saved using @DynamoDBDocument

I am trying to save a List<CustomObject> using the @DynamoDBDocument but it gives me a DynamoDBMappingException : could not unconvert attribute.

Here is what my Entity class looks like -

@lombok.Data
@DynamoDBTable(tableName = "carTable")
public class Car {
   @DynamoDBHashKey(attributeName = "name")
   private carName;

   @DynamoDBRangeKey(attributeName = "model")
   private carModel;

   @DynamoDBAttribute(attributeName = "manufacturers")
   private List<Manufacturer> manufacturers;
}

The Manufacturer class looks like -

@lombok.Data
@DynamoDBDocument
public class Manufacturer {
    @DynamoDBAttribute
    private String manufacturerName;
}

When using this entity and saving the values into the table it saves properly as JSON, but when retrieving it, an exception is thrown -

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Car[manufacturers]; could not unconvert attribute
at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperTableModel.unconvert(DynamoDBMapperTableModel.java:271)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.privateMarshallIntoObject(DynamoDBMapper.java:456)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:422)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:433)
[junit]     at com.amazonaws.services.dynamodbv2.datamodeling.AbstractDynamoDBMapper.load(AbstractDynamoDBMapper.java:85)

What am I missing here, do I need to add some sort of TypeConverter or Marshaller here?

like image 716
Manthan Jamdagni Avatar asked Jul 28 '17 11:07

Manthan Jamdagni


People also ask

Which of the data type is not supported by DynamoDB?

Unlike conventional relational databases, DynamoDB does not natively support a date and time data type.

What is attribute in DynamoDB?

In Amazon DynamoDB, an item is a collection of attributes. Each attribute has a name and a value. An attribute value can be a scalar, a set, or a document type. For more information, see Amazon DynamoDB: How it works. DynamoDB provides four operations for basic create, read, update, and delete (CRUD) functionality.

What are the three attributes of DynamoDB?

DynamoDB uses three basic data model units, Tables, Items, and Attributes. Tables are collections of Items, and Items are collections of Attributes. Attributes are basic units of information, like key-value pairs.

Can we store list in DynamoDB?

DynamoDB has support for storing complex data types like lists, sets or maps (aka dictionaries/hash tables). This capability allows for flexible usage patterns.


3 Answers

A no-args constructer was required to unconvert the attribute, adding @lombok.NoArgsConstructor to the Manufacturer class solved my problem.

like image 141
Manthan Jamdagni Avatar answered Oct 10 '22 02:10

Manthan Jamdagni


I don't think you need a custom converter here, it looks like that DynamoDB has some issues with converting DynamoDB item into a Java object.

Check:

  • Data in DynamoDB matches your object fields. Check if types are matching as well

  • What if you define setters explicitly? Maybe Lombok is a culprit here?

Also it is odd that you do not specify a table name here:

@DynamoDBTable(tableName)
public class Car {
...
}
like image 37
Ivan Mushketyk Avatar answered Oct 10 '22 02:10

Ivan Mushketyk


In my case, the issue was that the setters were missing

This was because I was using Lombok with @Builder and @Getter annotations only

like image 38
Rahul Midha Avatar answered Oct 10 '22 03:10

Rahul Midha