Preserve empty objects with @DynamoDbPreserveEmptyObject - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Preserve empty objects with @DynamoDbPreserveEmptyObject

If you save a bean to Amazon DynamoDB with empty objects and you want the SDK to recreate the empty objects upon retrieval, annotate the getter of the inner bean with @DynamoDbPreserveEmptyObject.

To illustrate how the annotation works, the code example uses the following two beans.

The following data class contains two InnerBean fields. The getter method, getInnerBeanWithoutAnno(), is not annotated with @DynamoDbPreserveEmptyObject. The getInnerBeanWithAnno() method is annotated.

@DynamoDbBean public class MyBean { private String id; private String name; private InnerBean innerBeanWithoutAnno; private InnerBean innerBeanWithAnno; @DynamoDbPartitionKey public String getId() { return id; } public void setId(String id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } public InnerBean getInnerBeanWithoutAnno() { return innerBeanWithoutAnno; } public void setInnerBeanWithoutAnno(InnerBean innerBeanWithoutAnno) { this.innerBeanWithoutAnno = innerBeanWithoutAnno; } @DynamoDbPreserveEmptyObject public InnerBean getInnerBeanWithAnno() { return innerBeanWithAnno; } public void setInnerBeanWithAnno(InnerBean innerBeanWithAnno) { this.innerBeanWithAnno = innerBeanWithAnno; } @Override public String toString() { return new StringJoiner(", ", MyBean.class.getSimpleName() + "[", "]") .add("innerBeanWithoutAnno=" + innerBeanWithoutAnno) .add("innerBeanWithAnno=" + innerBeanWithAnno) .add("id='" + id + "'") .add("name='" + name + "'") .toString(); } }

Instances of the following InnerBean class are fields of MyBean and are initialized as empty objects in the example code.

@DynamoDbBean public class InnerBean { private String innerBeanField; public String getInnerBeanField() { return innerBeanField; } public void setInnerBeanField(String innerBeanField) { this.innerBeanField = innerBeanField; } @Override public String toString() { return "InnerBean{" + "innerBeanField='" + innerBeanField + '\'' + '}'; } }

The following code example saves a MyBean object with initialized inner beans to DynamoDB and then retrieves the item. The logged output shows that the innerBeanWithoutAnno is not initialized, but innerBeanWithAnno has been created.

public MyBean preserveEmptyObjectAnnoUsingGetItemExample(DynamoDbTable<MyBean> myBeanTable) { // Save an item to DynamoDB. MyBean bean = new MyBean(); bean.setId("1"); bean.setInnerBeanWithoutAnno(new InnerBean()); // Instantiate the inner bean. bean.setInnerBeanWithAnno(new InnerBean()); // Instantiate the inner bean. myBeanTable.putItem(bean); GetItemEnhancedRequest request = GetItemEnhancedRequest.builder() .key(Key.builder().partitionValue("1").build()) .build(); MyBean myBean = myBeanTable.getItem(request);; // Output 'MyBean[innerBeanWithoutAnno=null, innerBeanWithAnno=InnerBean{innerBeanField='null'}, id='1', name='null']'. return myBean; }

You can use the following StaticTableSchema version of the table schemas in place of the annotations on the beans.

public static TableSchema<MyBean> buildStaticSchemas() { StaticTableSchema<InnerBean> innerBeanStaticTableSchema = StaticTableSchema.builder(InnerBean.class) .newItemSupplier(InnerBean::new) .addAttribute(String.class, a ->"innerBeanField") .getter(InnerBean::getInnerBeanField) .setter(InnerBean::setInnerBeanField)) .build(); return StaticTableSchema.builder(MyBean.class) .newItemSupplier(MyBean::new) .addAttribute(String.class, a ->"id") .getter(MyBean::getId) .setter(MyBean::setId) .addTag(primaryPartitionKey())) .addAttribute(String.class, a ->"name") .getter(MyBean::getName) .setter(MyBean::setName)) .addAttribute(EnhancedType.documentOf(InnerBean.class, innerBeanStaticTableSchema), a ->"innerBean1") .getter(MyBean::getInnerBeanWithoutAnno) .setter(MyBean::setInnerBeanWithoutAnno)) .addAttribute(EnhancedType.documentOf(InnerBean.class, innerBeanStaticTableSchema, b -> b.preserveEmptyObject(true)), a ->"innerBean2") .getter(MyBean::getInnerBeanWithAnno) .setter(MyBean::setInnerBeanWithAnno)) .build(); }