java - DynamoDBMappingException : no HASH key value present

标签 java amazon-dynamodb dynamo-local

我有一个名为 SampleEntity 的类,即 POJO,它将帮助我创建我的 dynamoDB 表。哈希键和范围键已在 POJO 对象中明确定义,但我仍然得到一个异常,即哈希键未定义

    @DynamoDBTable(tableName = "sampletable1")
    public class SampleEntity  {

    public static final String HASH_KEY = "f1_hash";
    public static final String RANGE_KEY = "f2_range";

    @DynamoDBAttribute(attributeName = HASH_KEY)
    @DynamoDBHashKey
    private Integer feild1;

    @DynamoDBAttribute(attributeName = RANGE_KEY)
    @DynamoDBRangeKey
    private String field2;

    @DynamoDBAttribute(attributeName = "f3")
    private String feild3;

    @DynamoDBAttribute(attributeName = "f4")
    private String feild4;

    @DynamoDBAttribute(attributeName = "f5")
    private String feild5;


    public Integer getFeild1() {
        return feild1;
    }

    public void setFeild1(Integer feild1) {
        this.feild1 = feild1;
    }

    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    public String getFeild3() {
        return feild3;
    }

    public void setFeild3(String feild3) {
        this.feild3 = feild3;
    }

    public String getFeild4() {
        return feild4;
    }

    public void setFeild4(String feild4) {
        this.feild4 = feild4;
    }

    public String getFeild5() {
        return feild5;
    }

    public void setFeild5(String feild5) {
        this.feild5 = feild5;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SampleEntity)) return false;

        SampleEntity that = (SampleEntity) o;

        if (!getFeild1().equals(that.getFeild1())) return false;
        if (!getField2().equals(that.getField2())) return false;
        if (!getFeild3().equals(that.getFeild3())) return false;
        if (!getFeild4().equals(that.getFeild4())) return false;
        return getFeild5().equals(that.getFeild5());
    }

    @Override
    public int hashCode() {
        int result = getFeild1().hashCode();
        result = 31 * result + getField2().hashCode();
        result = 31 * result + getFeild3().hashCode();
        result = 31 * result + getFeild4().hashCode();
        result = 31 * result + getFeild5().hashCode();


           return result;
        }
}

这是我的类,我正在对该类发出创建表请求,但我收到 DynamoDBMappingException,即不存在 HASH 键值。

server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", "8005"});
server.start();
dynamoDBClient = new AmazonDynamoDBClient(new BasicAWSCredentials("any", "thing")).withEndpoint("http://localhost:8005");

dbMapper = new DynamoDBMapper(dynamoDBClient);

CreateTableRequest createTableRequest = ddbMapper.generateCreateTableRequest(SampleEntity.class);
createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L));
        dynamoDBClient.createTable(createTableRequest);
        SampleLoginEntity data= new SampleLoginEntity();
        data.setLogin(123);
        data.setField2("range");
        data.setFeild3("abc");
        dbMapper.save(data);

最佳答案

我发现有两个可能的问题(其中一个是我最近遇到的),但您的设置与我的略有不同。

您在单个项目上同时使用 @DynamoDBAttribute 和 @DynamoDBHashKey - 这是不必要的,并且可能会弄乱它,尽管我现在没有时间测试它。你应该能够这样做,@DynamoDBHashKey(attributeName=HASH_KEY)你会没事的。我认为,您可能会将一个属性声明为“f1_hash”,并将一个哈希键声明为“field1”,两者都映射到相同的内部值(尽管我可能是错的)。

我遇到的问题实际上是由于此错误消息的措辞非常糟糕 - 当您调用 dbMapper.save() 时它会抛出此异常具有哈希键值设置为 null 的对象,但如果您的 setLogin()应该是setField1()这不应该是这里的问题。

关于java - DynamoDBMappingException : no HASH key value present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405850/

相关文章:

java - 具有不同轴的嵌套 BoxLayout

aws-lambda - dynamodb keyconditionexpression 与 filterexpression 成本?

python - 将 Json 放入 DynamoDB 项目

amazon-web-services - 由于命名约定,使用 DynamoDB AWS SDK [No Mapping for Hash Key] 时出错

java - AWS Local DynamoDB 请求中包含的安全 token 无效

java - Dynamodb 无法创建表

java - JFileChooser组件显示怪异

java - 检查两个数组是否按某种顺序具有相同的元素

java - 通过 JSMPP 提供 SMPP 连接

node.js - "Missing required key ' Dynamodb的Get操作中的key ' in params"