DynamoDB没有接口,也没有发现DynamoDBHashKey表示法错误

时间:2017-08-30 09:06:12

标签: android-studio amazon-dynamodb

我已经看过其他类似的问题,所有这些问题都错误地将@DynamoDBHashKey编写为@DynamoDBIndexHashKey,但我没有。

我不想要排序键或索引。我已经用主键创建了我的表作为电子邮件。

NewUser.Java

import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBAttribute;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBHashKey;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBTable;

mHandler.post(new Runnable() {
            public void run() {
                EditText name=(EditText)findViewById(R.id.RegisterName);
                EditText email=(EditText)findViewById(R.id.Email);
                Logindatabase login = new Logindatabase();
                login.setEmail(email.getText().toString());
                login.setName(name.getText().toString());
                login.setStatus();
                mapper.save(login); //Exception is thrown here
                Toast.makeText(NewUser.this, "Sent", Toast.LENGTH_SHORT).show();
            }
        });

Logindatabase.java

@DynamoDBTable(tableName = "Login")
public class Logindatabase {
private String email;
private String Name;
private String password;
private String Status;
@DynamoDBHashKey(attributeName = "Email")
public void setEmail(String email){
    this.email=email;
}
public String getEmail(){return email;}
@DynamoDBAttribute(attributeName = "Name")
public void setName(String Name){
    this.Name=Name;
}
public String getName(){
    return Name;
}
@DynamoDBAttribute(attributeName = "Status")
public void setStatus()
{
    Status="Pending";
}
}

logcat的

E/AndroidRuntime: FATAL EXCEPTION: main
com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMappingException: No interface com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBHashKey annotation found in class class com.example.sherry.empapp.Logindatabase
                                                                           at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.needAutoGenerateAssignableKey(DynamoDBMapper.java:720)
                                                                           at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:780)
                                                                           at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:688)
                                                                           at com.example.sherry.empapp.NewUser$1.run(NewUser.java:67)
                                                                           at android.os.Handler.handleCallback(Handler.java:730)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                           at android.os.Looper.loop(Looper.java:176)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:525)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                           at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

应为获取方法添加@DynamoDBHashKey注释,而不是设置方法。

另外,请确保散列键属性名称与模型类(即模型类中定义)匹配,并使用DynamoDB表散列键属性定义(并且区分大小写)。

@DynamoDBHashKey(attributeName = "email")
public String getEmail() {
    return email;
}

答案 1 :(得分:0)

如果你使用lombok来避免创建getter和setter,注释@DynamoDBHashKey可以直接在字段和setter中使用ElementType.FIELD, ElementType.METHOD

import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBAttribute;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBHashKey;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBRangeKey;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBTable;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBVersionAttribute;

import lombok.Builder;
import lombok.Data;

@Builder
@Data
@DynamoDBTable(tableName = "ProductTable")
public class Product {

@DynamoDBHashKey
private String userName;

@DynamoDBRangeKey
private Long range;

@DynamoDBVersionAttribute
private Long version;

@DynamoDBAttribute(attributeName = "name")
private String name;
}

在这种情况下,仍然需要setter,但是这些是由lombok @Data注释添加的。