AWS DynamoDB Java SDK alternative to annotations

dynamodbignore example
dynamodb unmarshal java
dynamodb datamodeling
dynamodb object mapping
dynamodb orm
dynamodbmapper map
dynamodbobjectmapper
dynamodbmapper lombok

I want to avoid using DynamoDB Annotations on a class but still be able to use a mapper to write and read from DynamoDB. I am using the Java SDK to read and write from DynamoDB (using the provided DynamoDBMapper.

Say I have this Model.java

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

    private Integer id;
    private String title;

    @DynamoDBHashKey(attributeName="ModelId")  
    public Integer getId() { return id;}
    public void setId(Integer id) {this.id = id;}

    @DynamoDBAttribute(attributeName="Title")  
    public String getTitle() {return title; }
    public void setTitle(String title) { this.title = title; }
}

I would write to it as usual

Model model = new Model();
//... random information

DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(model);

This is fine but my domain object is littered with DynamoDB specific annotations. Is there a way to instead have a class that wrapped the domain object (without any annotations). In the case below I would be looking for an effective implementation of DynamoDBModelWrapper

public class Model {
    private Integer id;
    private String title;

    public Integer getId() { return id;}
    public void setId(Integer id) {this.id = id;}

    public String getTitle() {return title; }
    public void setTitle(String title) { this.title = title; }
}

public class DynamoDBModelWrapper extends Model { ... }

Model model = new Model();
//... random information

DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(DyanamoDBModelWrapper(model));

Cheers and thank you

This seems like a really bad idea. You would effectively have to force all of your table keys and indexes to have the same name. The model-wrapper can't just apply the same annotation to each model attribute, it has to know which annotation to apply to which attribute, which would mean still applying annotations to the model class, or since this is not allowed, forcing the model class to name attributes in a particular way. For example your primary hash key attribute might have to be called primaryhashkey.

Also you would not be able to use some of the really powerful parts of DyanmoDBMapper like complex and arbitrary data mapping.

One thing to mention is that you could tidy things up at least. You have some superfluous text in your model class. You only need to specify attributeName if the name of your model attribute is different to the table attribute. If they match you can simply omit it. The same is true for the model class name and the tableName. So you can do:

Table schema:
TableName: Model
Partition Key Attribute: id
Attribute: title

And then your model class would look like this

@DynamoDBTable
public class Model {

    private Integer id;
    private String title;

    @DynamoDBHashKey  
    public Integer getId() { return id;}
    public void setId(Integer id) {this.id = id;}

    @DynamoDBAttribute 
    public String getTitle() {return title; }
    public void setTitle(String title) { this.title = title; }
}

The annotations are still there but it looks a lot neater.

Java Annotations for DynamoDB - Amazon DynamoDB, For the corresponding Javadoc documentation, see Annotation Types Summary in the AWS SDK for Java API Reference. Note. In the following annotations, only​  Java Annotations for DynamoDB. This section describes the annotations that are available for mapping your classes and properties to tables and attributes in Amazon DynamoDB. For the corresponding Javadoc documentation, see Annotation Types Summary in the AWS SDK for Java API Reference.

It's not a great solution (IMO), so was hoping someone had suggested better, but I did the following:

public class Model {
    private Integer id;
    private String title;

    public Integer getId() { return id;}
    public void setId(Integer id) {this.id = id;}

    public String getTitle() {return title; }
    public void setTitle(String title) { this.title = title; }
}

@DynamoDBTable(tableName="Model")
public class DynamoDBModelWrapper  {

    private final Model model;

    DynamoDBModelWrapper() {
        this.model = new Model();
    }
    DynamoDBModelWrapper(final Model model) {
        this.model = model;
    }

    @DynamoDBHashKey(attributeName="ModelId")  
    public Integer getId() { return model.id;}
    public void setId(Integer id) {model.id = id;}

    @DynamoDBAttribute(attributeName="Title")  
    public String getTitle() {return model.title; }
    public void setTitle(String title) { model.title = title; }
}

You can then instantiate a DynamoDBModelWrapper when you save / load from DynamoDB and use the object without any of the Dynamo specific annotations the rest of the time.

Model model = new Model();
//... random information

DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(new DynamoDBModelWrapper(model));

DynamoDBMapper Class - Amazon DynamoDB, A mapper class for this index is shown in the query section. It uses the @​DynamoDBIndexHashKey and @DynamoDBIndexRangeKey annotations to specify the  The AWS SDK for Java provides a DynamoDBMapper class, allowing you to map your client-side classes to Amazon DynamoDB tables.

DynamoDB Client vs DynamoDBMapper - ananthsrinivas, Recently I came across a situation to access Dynamo DB (DDB) for one of my requirements. tables using the DynamoDBMapper class in the AWS SDK for Java. annotations required for DDBMapper to understand what goes into the table. Programming with DynamoDB and the AWS SDKs This section covers developer-related topics. If you want to run code examples instead, see Running the Code Examples in This Developer Guide .

Getting started with Amazon DynamoDB and Java, Getting started with Amazon DynamoDB and Java First of all, we need to grab spring boot and AWS dynamodb SDK as our local dependencies: One might consider an annotation that would specify it, but this is probably a bad Local index — Basically an alternative range key for the table hash key. This section provides examples of programming DynamoDB using the AWS SDK for Java. Note. The examples include only the code needed to demonstrate each technique. The complete example code is available on GitHub. From there, you can download a single source file or clone the repository locally to get all the examples to build and run.

Dynamic Table Name in DynamoDB with Java DynamoMapper, The problem is that the Java object representing a DynamoDB entity This will ignore the table name defined by the @DynamoDBTable annotation in the In the AWS SDK version we used (1.5.4) there was no overloaded  The AWS SDK for Java provides a DynamoDBMapper class, allowing you to map your client-side classes to Amazon DynamoDB tables. To use DynamoDBMapper, you define the relationship between items in a DynamoDB table and their corresponding object instances in your code.

Using the AWS DynamoDB Low-Level Java API – Spring Boot Rest , Using the AWS DynamoDB Low-Level Java API – Spring Boot Rest Application DocumentDB, which we could use as an alternative to DynamoDB. upon DynamoDB via annotations mapping the class to the Widgets table. The 2.0 version of the SDK is a major rewrite of the 1.11 code base. Built for support for Java 8+, The AWS SDK for Java 2.0 adds several frequently requested features, like support for non-blocking I/O, better start-up performance and automatic iteration over paginated responses.

Comments
  • Cheers Stu, this makes sense and I chose to keep using annotations