Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB inheritance no mapping parent hash

I have 3 class:

public abstract class Animal{
    private String id;
    private String name;

    @DynamoDBHashKey(attributeName = "Id")
    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;
    }
}

Then a class Cat:

@DynamoDBTable(tableName = "Cat")
public class Cat extends Animal{
    private String purr;
    @DynamoDBAttribute(attributeName = "Purr")
    public String getPurr() {
        return purr;
    }
    public void setPurr(String purr) {
        this.purr = purr;
    }
}

And a similar class Dog that extends Animal and use another table called Dog: @DynamoDBTable(tableName = "Dog")

The problem is that when I try to save a Cat, I get the error:

Cat: does not map HASH key on model

But Cat should have the hash key of his superclass.

I can solve moving the annotation: @DynamoDBTable(tableName = "Cat") from the class Cat to the superclass, it works to save Cat. I still have not tested if this will cause problem to Dog, but I don't think so, because it will be overwrited by @DynamoDBTable(tableName = "Dog")

But this solution seem ugly to me. Is there a nicer solution? (I would consider to merge both the tables Dog and Cat to just one table called Animal.)

like image 340
Accollativo Avatar asked Dec 28 '25 23:12

Accollativo


1 Answers

Put this annotation on superclass:

@DynamoDBDocument
public abstract class Animal{
//...
}
like image 170
Accollativo Avatar answered Dec 30 '25 11:12

Accollativo