Reputation: 3012
I would like to create a general DynamoDB schema, which shall be shared by a number of DynamoDb table classes:
@DynamoDBTable(tableName = "please_dont_create_me!")
public abstract class Schema {
@DynamoDBHashKey
private String id;
private String value;
/* setters and getters .. */
}
@DynamoDBTable(tableName = "table_1")
public class FirstTable extends Schema {
}
// Woops, I forgot to annotate with @DynamoDBTable
public class SecondTable extends Schema {
}
Saving an instance of FirstTable
works as expected:
FirstTable ft = new FirstTable();
ft.setId("occupation");
ft.setValue("artist");
mapper.save(ft); // Saves (occupation, artist) to 'table_1'
Now saving SecondTable
, which I forgot to annotate with @DynamoDBTable
:
SecondTable st = new SecondTable();
st.setId("hobby");
st.setValue("drawing");
mapper.save(st); // Saves (hobby, drawing) to 'please_dont_create_me!'
Is it possible to create Schema
in such a way so as to avoid this situation?
My thoughts:
Schema
must be annotated with @DynamoDBTable
, otherwise the fields it defines are not picked up (when attempting to save FirstTable, an exception is thrown about a missing hashKey, for example)Schema
as an interface instead would not really work*. The main flaw is that @DynamoDBHashKey
and other field annotations are not inherited.*I have not actually tried using an interface
Upvotes: 1
Views: 5712
Reputation: 755
You want to make your table selection dynamic with same pojo class. This is how I did.
AmazonDynamoDBClient client = new AmazonDynamoDBClient();
DynamoDBMapper mapper = new DynamoDBMapper(client);
client.setEndpoint("http://localhost:8000/");
String tableName="User";
User user = new User();
user.setName("Ohelig");
TableNameOverride config = new DynamoDBMapperConfig.TableNameOverride(tableName);
mapper.save(user, new DynamoDBMapperConfig(config));
Upvotes: 1
Reputation: 10052
What do you expect when saving SecondTable with no @DynamoDBTable annotation?
If the goal is to play with the table name I suggest you dive into TableNameResolver.
Upvotes: 1