DynamoDBMappingException:不存在HASH键值

时间:2017-04-14 05:43:35

标签: 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;
        }
}

这是我的类,我在这个类上发出了一个创建表请求,但是我得到了没有HASH键值的DynamoDBMappingException。

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);

1 个答案:

答案 0 :(得分:2)

我可以看到两个可能的问题(我最近遇到过一个问题),但你的设置与我的设置略有不同。

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

我遇到的问题实际上是这个错误消息措辞真的很差的结果 - 当你用一个散列键值设置为null的对象调用dbMapper.save()时它会抛出这个异常,不过如果你的setLogin()应该是setField1(),这不应该是问题。