DynamoDB映射器无架构设计

时间:2018-09-20 11:04:26

标签: java amazon-web-services inheritance amazon-dynamodb

我正在尝试将DynamoDB用于其无模式功能,并尝试适合如下所示的数据模型。在这种情况下,我试图重载表的主键以将相似的数据放入单个表中。

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

@Getter(AccessLevel.PUBLIC)
@Setter(AccessLevel.PROTECTED)
@ToString
@SuperBuilder
@DynamoDBTable(tableName = "POC")
public class ParentModel {
  @DynamoDBHashKey(attributeName = "partitionKey")
  private String partitionKey;
  @DynamoDBRangeKey(attributeName = "sortKey")
  private String sortKey;

  @Getter
  @Setter
  @ToString
  @SuperBuilder
  @DynamoDBTable(tableName = "POC")
  public static class ChildModelOne extends ParentModel {
    @DynamoDBAttribute(attributeName = "fieldOne")
    private String fieldOne;
    @DynamoDBAttribute(attributeName = "fieldTwo")
    private String fieldTwo;
  }

  @Getter
  @Setter
  @ToString
  @SuperBuilder
  @DynamoDBTable(tableName = "POC")
  public static class ChildModelTwo extends ParentModel {
    @DynamoDBAttribute(attributeName = "fieldThree")
    private String fieldThree;
    @DynamoDBAttribute(attributeName = "fieldFour")
    private DocuModel fieldFour;

    @Getter
    @Setter
    @ToString
    @SuperBuilder
    @DynamoDBDocument
    public static class DocuModel {
      @DynamoDBAttribute(attributeName = "fieldFive")
      private String fieldFive;
      @DynamoDBAttribute(attributeName = "fieldSix")
      private String fieldSix;
    }
  }
}

但是,DynamoDBMapper引发了如下的Mapping异常; com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: ChildModelOne; no mapping for HASH key

DynamoDB是否以上述方式支持模型继承?如果可以,那我要去哪里错了?

0 个答案:

没有答案