WaelJ
WaelJ

Reputation: 3012

Force override of DynamoDB table name

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:

*I have not actually tried using an interface

Upvotes: 1

Views: 5712

Answers (2)

Yanish Pradhananga
Yanish Pradhananga

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

Chen Harel
Chen Harel

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

Related Questions