2017-04-14 32 views
2

我也即POJO,这将帮助我建立我dynamoDB table.The哈希键和范围键已经明确规定在POJO对象名为SampleEntity类,但我仍然得到一个异常散列关键是不被定义DynamoDBMappingException:目前还没有散列键值

@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是不存在散列键值。

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

回答

2

我可以看到两个可能的问题(我最近碰到的一个问题),但是您的设置与我的设置稍有不同。

您在单个项目上同时使用@DynamoDBAttribute和@DynamoDBHashKey - 这不是必要的,可能会搞砸了,尽管我现在没有时间去测试它了。你应该能够只是做,@DynamoDBHashKey(attributeName=HASH_KEY),你会没事的。我觉得是,你可能会宣布一个属性为“f1_hash”,并命名为“字段1”的哈希键,都映射到相同的内在价值(虽然我可能是错的)。

虽然我遇到的问题其实是这个错误消息的结果很糟糕 - 它会抛出这个异常,当你打电话给dbMapper.save()与对象的散列键值设置为null,但如果你的setLogin()假设为setField1(),这不应该成为这里的问题。