简体   繁体   English

从 dynamoDB 表中删除数据时出错

[英]Error while deleting data from dynamoDB table

I am trying to delete the data from my DynamoDB table based on the Id(HashKey).我正在尝试根据 Id(HashKey) 从我的 DynamoDB 表中删除数据。

Association.java协会.java

@DynamoDBTable(tableName = "Association")
public class Association {

    private String id;
    private String name;
    private String adminName;
    private String email;
    private String url;
    private String contactNumber;
    private String password;

    public Association() { }

    public Association(String name, String adminName, String email, String url,
                       String contactNumber, String password) {
        this.name = name;
        this.adminName = adminName;
        this.email = email;
        this.url = url;
        this.contactNumber = contactNumber;
        this.password = password;
    }

    public Association(String id, String name, String adminName, String email, String url,
                       String contactNumber, String password) {
        this.id = id;
        this.name = name;
        this.adminName = adminName;
        this.email = email;
        this.url = url;
        this.contactNumber = contactNumber;
        this.password = password;
    }

    @DynamoDBHashKey(attributeName = "Id")
    @DynamoDBAutoGeneratedKey
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @DynamoDBAttribute(attributeName="Name")
    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    @DynamoDBAttribute(attributeName="AdminName")
    public String getAdminName() { return adminName; }

    public void setAdminName(String adminName) { this.adminName = adminName; }

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

    public void setEmail(String email) { this.email = email; }

    @DynamoDBAttribute(attributeName="Url")
    public String getUrl() { return url; }

    public void setUrl(String url) { this.url = url; }

    @DynamoDBAttribute(attributeName="ContactNumber")
    public String getContactNumber() { return contactNumber; }

    public void setContactNumber(String contactNumber) { this.contactNumber = contactNumber; }

    @DynamoDBAttribute(attributeName="Password")
    public String getPassword() { return password; }

    public void setPassword(String password) { this.password = password; }

}

AssociationRepository.java:- AssociationRepository.java:-

private AmazonDynamoDBClient getDynamoDBClient(){
        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setRegion(Region.getRegion(REGION));
        client.setEndpoint(EndPoint);
        return client;
    }

private void deleteAssociation(String id) {
        try {
            DynamoDBConfig dynamoDBConfig = new DynamoDBConfig();
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBConfig.getDBClient());
            Association associationToDelete = new Association();
            associationToDelete.setId(id);
            // Delete the item.
            mapper.delete(associationToDelete);
        } catch (Exception exception) {
            System.out.println(exception);
            throw exception;
        }
    }

在此处输入图片说明 在此处输入图片说明

I am getting the below error:-我收到以下错误:-

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: No method annotated with interface com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey for class class Association

I have searched the AWS docs for this but could not find any solution that fixes this.Does anyone of you ever faced this issue?我已经为此搜索了 AWS 文档,但找不到任何解决此问题的解决方案。你们中有人遇到过这个问题吗?

The issue is the same I mentioned on your other post, in DynamoDB you can't work an item using attributes which are not the primary key (Partition Key only in your scenario).问题与我在您的另一篇文章中提到的相同,在 DynamoDB 中,您无法使用不是主键的属性(仅在您的场景中使用分区键)处理项目。

Moreover, to delete an item it is mandatory to do it by its primary key, so in your case the attribute Name.此外,要删除一个项目,必须通过其主键来完成,因此在您的情况下是属性名称。

Try to do same operation by setting the name, the error is not very descriptive so maybe there is another underlying issue.尝试通过设置名称来执行相同的操作,错误不是很具有描述性,因此可能存在另一个潜在问题。 However, if you don't meet above requirement it will never work.但是,如果您不满足上述要求,它将永远无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM