简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM