java Amazon DynamoDB 映射枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12589654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:27:21  来源:igfitidea点击:

Amazon DynamoDB mapping enums

javaamazon-web-servicesamazon-dynamodb

提问by user1007895

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.

我需要为 Amazon DynamoDB 映射一个 User 类。User 对象的属性之一是 AccountType(一个枚举)。我该如何处理?下面是我尝试过的枚举和代码。

public enum AccountType {
    TYPE_A,
    TYPE_B
}

-

——

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

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Steffen Opel

The high-level API (the Object Persistence model) for Amazon DynamoDBprovided by the AWS SDK for Javadoesn't support enumyet, see Supported Data Types:

AWS SDK for Java提供的Amazon DynamoDB的高级 API(对象持久性模型)尚不支持,请参阅支持的数据类型enum

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

Amazon DynamoDB 支持以下原始数据类型和原始包装类。

  • 细绳
  • 布尔值,布尔值
  • 字节,字节
  • 日期(作为 ISO8601 毫秒精度字符串,转换为 UTC)
  • 日历(作为 ISO8601 毫秒精度字符串,转换为 UTC)
  • 好长好长
  • 整数,int
  • 双,双
  • 漂浮,漂浮
  • 大十进制
  • 大整数

However, Amazon DynamoDB supports arbitrary data typesin 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 Modelfor details:

但是,Amazon DynamoDB原则上支持任意数据类型,因此您可能能够解决该限制,请参阅使用 AWS SDK for Java Object Persistence Model 映射任意数据与 Amazon DynamoDB 以了解详细信息:

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

除了支持的 Java 类型 [...] 之外,您还可以在应用程序中使用没有直接映射到 Amazon DynamoDB 类型的类型。要映射这些类型,您必须提供一个实现,将复杂类型转换为 String 的实例,反之亦然,并使用 @DynamoDBMarshalling 注释类型注释复杂类型访问器方法。[...]

回答by H6.

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

AWS 开发工具包支持使用特殊注释DynamoDBTypeConvertedEnum将枚举转换为字符串。

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