2017-04-14 31 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(),這不應該成爲這裏的問題。