user1007895
user1007895

Reputation: 3965

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.

Upvotes: 32

Views: 46182

Answers (2)

H6_
H6_

Reputation: 32808

The AWS SDK supports the special annotation DynamoDBTypeConvertedEnum to convert an enum into a string.

@DynamoDBTypeConvertedEnum
@DynamoDBAttribute(attributeName="AccountType")
public AccountType getAccountType() {
    return accountType;
}

Upvotes: 80

Steffen Opel
Steffen Opel

Reputation: 64741

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. [...]

Upvotes: 19

Related Questions