本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
避免保存嵌套對象的 null 屬性
透過套用註解將資料類別物件儲存至 DynamoDB 時,您可以略過巢狀物件的空屬性。@DynamoDbIgnoreNulls
相比之下,具有 null 值的頂層屬性永遠不會儲存到資料庫中。
為了說明註釋的工作原理,代碼示例使用以下兩個 bean。
下面的數據類包含兩個InnerBean
字段。吸氣方法,getInnerBeanWithoutAnno()
,沒有註釋。該getInnerBeanWithIgnoreNullsAnno()
方法用註釋。@DynamoDbIgnoreNulls
@DynamoDbBean public class MyBean { private String id; private String name; private InnerBean innerBeanWithoutAnno; private InnerBean innerBeanWithIgnoreNullsAnno; @DynamoDbPartitionKey public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public InnerBean getInnerBeanWithoutAnno() { return innerBeanWithoutAnno; } public void setInnerBeanWithoutAnno(InnerBean innerBeanWithoutAnno) { this.innerBeanWithoutAnno = innerBeanWithoutAnno; } @DynamoDbIgnoreNulls public InnerBean getInnerBeanWithIgnoreNullsAnno() { return innerBeanWithIgnoreNullsAnno; } public void setInnerBeanWithIgnoreNullsAnno(InnerBean innerBeanWithAnno) { this.innerBeanWithIgnoreNullsAnno = innerBeanWithAnno; } @Override public String toString() { return new StringJoiner(", ", MyBean.class.getSimpleName() + "[", "]") .add("innerBeanWithoutAnno=" + innerBeanWithoutAnno) .add("innerBeanWithIgnoreNullsAnno=" + innerBeanWithIgnoreNullsAnno) .add("id='" + id + "'") .add("name='" + name + "'") .toString(); } }
下列InnerBean
類別的執行個體是的欄位,MyBean
並在下列範例程式碼中使用。
@DynamoDbBean public class InnerBean { private String innerBeanFieldString; private Integer innerBeanFieldInteger; public String getInnerBeanFieldString() { return innerBeanFieldString; } public void setInnerBeanFieldString(String innerBeanFieldString) { this.innerBeanFieldString = innerBeanFieldString; } public Integer getInnerBeanFieldInteger() { return innerBeanFieldInteger; } public void setInnerBeanFieldInteger(Integer innerBeanFieldInteger) { this.innerBeanFieldInteger = innerBeanFieldInteger; } @Override public String toString() { return new StringJoiner(", ", InnerBean.class.getSimpleName() + "[", "]") .add("innerBeanFieldString='" + innerBeanFieldString + "'") .add("innerBeanFieldInteger=" + innerBeanFieldInteger) .toString(); } }
下列程式碼範例會建立InnerBean
物件,並且只會使用值來設定其兩個屬性中的一個。
public void ignoreNullsAnnoUsingPutItemExample(DynamoDbTable<MyBean> myBeanTable) { // Create an InnerBean object and give only one attribute a value. InnerBean innerBeanOneAttributeSet = new InnerBean(); innerBeanOneAttributeSet.setInnerBeanFieldInteger(200); // Create a MyBean instance and use the same InnerBean instance both for attributes. MyBean bean = new MyBean(); bean.setId("1"); bean.setInnerBeanWithoutAnno(innerBeanOneAttributeSet); bean.setInnerBeanWithIgnoreNullsAnno(innerBeanOneAttributeSet); Map<String, AttributeValue> itemMap = myBeanTable.tableSchema().itemToMap(bean, true); logger.info(itemMap.toString()); // Log the map that is sent to the database. // {innerBeanWithIgnoreNullsAnno=AttributeValue(M={innerBeanFieldInteger=AttributeValue(N=200)}), id=AttributeValue(S=1), innerBeanWithoutAnno=AttributeValue(M={innerBeanFieldInteger=AttributeValue(N=200), innerBeanFieldString=AttributeValue(NUL=true)})} // Save the MyBean object to the table. myBeanTable.putItem(bean); }
為了視覺化傳送至 DynamoDB 的低階資料,程式碼會在儲存物件之前記錄屬性對應。MyBean
記錄的輸出顯示輸出一個屬性,innerBeanWithIgnoreNullsAnno
innerBeanWithIgnoreNullsAnno=AttributeValue(M={innerBeanFieldInteger=AttributeValue(N=200)})
執行個innerBeanWithoutAnno
體會輸出兩個屬性。一個屬性的值為 200,另一個是空值屬性。
innerBeanWithoutAnno=AttributeValue(M={innerBeanFieldInteger=AttributeValue(N=200), innerBeanFieldString=AttributeValue(NUL=true)})
下列 JSON 表示法可讓您更輕鬆地查看儲存至 DynamoDB 的資料。
{ "id": { "S": "1" }, "innerBeanWithIgnoreNullsAnno": { "M": { "innerBeanFieldInteger": { "N": "200" } } }, "innerBeanWithoutAnno": { "M": { "innerBeanFieldInteger": { "N": "200" }, "innerBeanFieldString": { "NULL": true } } } }
您可以在地方數據類註釋使用以下StaticTableSchema
版本的表模式。
public static TableSchema<MyBean> buildStaticSchemas() { StaticTableSchema<InnerBean> innerBeanStaticTableSchema = StaticTableSchema.builder(InnerBean.class) .newItemSupplier(InnerBean::new) .addAttribute(String.class, a -> a.name("innerBeanFieldString") .getter(InnerBean::getInnerBeanFieldString) .setter(InnerBean::setInnerBeanFieldString)) .addAttribute(Integer.class, a -> a.name("innerBeanFieldInteger") .getter(InnerBean::getInnerBeanFieldInteger) .setter(InnerBean::setInnerBeanFieldInteger)) .build(); return StaticTableSchema.builder(MyBean.class) .newItemSupplier(MyBean::new) .addAttribute(String.class, a -> a.name("id") .getter(MyBean::getId) .setter(MyBean::setId) .addTag(primaryPartitionKey())) .addAttribute(String.class, a -> a.name("name") .getter(MyBean::getName) .setter(MyBean::setName)) .addAttribute(EnhancedType.documentOf(InnerBean.class, innerBeanStaticTableSchema), a -> a.name("innerBeanWithoutAnno") .getter(MyBean::getInnerBeanWithoutAnno) .setter(MyBean::setInnerBeanWithoutAnno)) .addAttribute(EnhancedType.documentOf(InnerBean.class, innerBeanStaticTableSchema, b -> b.ignoreNulls(true)), a -> a.name("innerBeanWithIgnoreNullsAnno") .getter(MyBean::getInnerBeanWithIgnoreNullsAnno) .setter(MyBean::setInnerBeanWithIgnoreNullsAnno)) .build(); }