javaamazon-web-servicesamazon-dynamodb

Amazon DynamoDB mapping enums


I need to map a User class for Amazon DynamoDB. One of the properties on the User object is AccountType (an enum). How do I handle this? Below is the enum and the code I have tried.

public enum AccountType {
    TYPE_A,
    TYPE_B
}

-

@DynamoDBAttribute(attributeName="AccountType")   *<< THIS FAILS*
public AccountType getAccountType() {
    return accountType;
}

Any help would be appreciated.


Solution

  • The high-level API (the Object Persistence model) for Amazon DynamoDB provided by the AWS SDK for Java doesn't support enum yet, see Supported Data Types:

    Amazon DynamoDB supports the following primitive data types and primitive wrapper classes.

    • String
    • Boolean, boolean
    • Byte, byte
    • Date (as ISO8601 millisecond-precision string, shifted to UTC)
    • Calendar (as ISO8601 millisecond-precision string, shifted to UTC)
    • Long, long
    • Integer, int
    • Double, double
    • Float, float
    • BigDecimal
    • BigInteger

    However, Amazon DynamoDB supports arbitrary data types in principle, so you might be able to work around that limitation, see Mapping Arbitrary Data with Amazon DynamoDB Using the AWS SDK for Java Object Persistence Model for details:

    In addition to the supported Java types [...], you can use types in your application for which there is no direct mapping to the Amazon DynamoDB types. To map these types, you must provide an implementation that converts your complex type to an instance of String and vice-versa and annotate the complex type accessor method using the @DynamoDBMarshalling annotation type. [...]