Introducing immutable class mapping for the Enhanced DynamoDB Consumer in AWS SDK for Java 2.x

We’re happy to announce that the enhanced DynamoDB shopper within the AWS SDK for Java 2.x now helps the mapping of immutable Java objects instantly with data in your DynamoDB tables. Beforehand, solely mutable ‘Java bean’ type objects had been supported.

Immutability in Java is a generally used type that enables builders to create lessons which can be side-effect free and due to this fact predictable in advanced and multi-threaded functions. Immutable objects in Java characteristically solely have getters (and no setters) and are constructed by passing all their properties without delay to the constructor. It is usually typical to have a separate mutable ‘builder’ class to help with gathering the initialization values for these properties.

Earlier than this launch, clients utilizing immutable type objects had no strategy to instantly map data they had been studying and writing from their DynamoDB tables to those objects. As an alternative, they had been pressured to work with bean objects and use elaborate workarounds, equivalent to copying and remodeling the objects learn by the improved shopper or wrapping them in an opaque container. Now the improved shopper can work instantly with the immutable lessons with the addition of a only a few easy annotations.

Fast walk-through for mapping an immutable class


Earlier than getting began with mapping immutable lessons, guarantee your SDK dependency is updated with all the most recent launched bug-fixes and options. For immutable object mapping help you should be utilizing model 2.14.10 or later. See our Java SDK Developer Information for the main points on the best way to handle the AWS Java SDK dependency in your venture.

Step 1 : Annotate your current immutable class

First, create and annotate an immutable class that can map to data in your DynamoDB desk. You might both observe the instance beneath or create your personal class. The immutable class you create ought to solely have getters, and there should be a static builder class that can be utilized to assemble situations of it.

Add the @DynamoDbImmutable annotation to the immutable class. You will need to go a parameter of ‘builder’ to the annotation with the worth set to the builder class you utilize to construct objects of your immutable class. An instance utilizing an inner-class of our immutable class ‘Report’ referred to as ‘Builder’ could be @DynamoDbImmutable(builder = Report.Builder.class).

Add different customary DynamoDB enhanced shopper annotations as required. These annotations are the identical and work in the identical manner as the present bean class annotations. At a minimal you should specify a @DynamoDbPrimaryKey. Different annotations are non-obligatory.

This instance exhibits an entire however minimally annotated immutable class that’s prepared to be used with the improved shopper:

@DynamoDbImmutable(builder = Report.Builder.class)
public class Report {
  non-public ultimate String id;
  non-public ultimate String attribute;

  non-public Report(Builder b) { =;
    this.attribute = b.attribute;

  public static Builder builder() {
    return new Builder();

  public String id() { return; }

  public String attribute() { return this.attribute; }

  public static ultimate class Builder {
    non-public String id;
    non-public String attribute;

    public Builder id(String id) { = id;
      return this;

    public Builder attribute(String attribute) {
      this.attribute = attribute;
      return this;

    public Report construct() {
      return new Report(this);

For extra details about what structurally qualifies a category for use as a DynamoDB annotated immutable class, please seek advice from “Working with immutable information lessons” part within the DynamoDB enhanced shopper information.

Step 2 : Assemble a TableSchema to your immutable class

Subsequent, create a TableSchema object for the immutable class you created within the earlier step. A TableSchema is an object that defines the construction and attributes of DynamoDB data and is aware of the best way to map them to a Java class.

To maintain issues easy, this instance makes use of a brand new static constructor technique in TableSchema that auto-detects and works with each annotated immutable lessons and annotated bean lessons:

TableSchema Report recordTableSchema = tableSchema.forClass(Report.class);

Step 3 : Create a DynamoDbTable useful resource object

Subsequent, affiliate an actual DynamoDB desk together with your TableSchema to create a DynamoDBTable useful resource object to execute instructions towards. This instance makes use of an actual DynamoDB desk named ‘my_table’:

DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.create();
DynamoDbTable<Report> recordTable = enhancedClient.desk("my_table", recordTableSchema);

Step 4 : Execute instructions towards the DynamoDbTable useful resource object

Any objects learn from the database will now be created as immutable objects utilizing the category you offered. You might also go immutable situations of this class to operations that write or replace data within the desk. For instance:

Report newRecord = Report.builder().id("123").attribute("foo").construct();

Report key = Report.builder().id("123").construct();
Report persistedRecord = recordTable.getItem(key);


On this weblog put up we confirmed you the best way to set-up and start mapping immutable lessons with the DynamoDB enhanced shopper. The improved shopper is open-source and resides in the identical repository because the AWS SDK for Java 2.0.

We hope you’ll discover this new function helpful. You’ll be able to at all times share your suggestions on our GitHub points web page or up-vote different concepts for options you’d additionally prefer to see within the DynamoDB enhanced shopper or the Java SDK typically.

(Visited 3 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Ask ChatGPT
Set ChatGPT API key
Find your Secret API key in your ChatGPT User settings and paste it here to connect ChatGPT with your Tutor LMS website.
Would love your thoughts, please comment.x