Dynamo DB UPDATE_SKIP_NULL_ATTRIBUTES behavior updating the unmodeled Number attributes to '0'

3.8k Views Asked by At

Dynamo table item

id : number
Name : string
age : number
Address : sting

Java pojo is

id : int
Name : String
age : int
Address : String

My DynamoMapperConfig is

dynamoDBMapperConfig = new DynamoDBMapperConfig.Builder()
            .withTableNameOverride(DynamoDBMapperConfig.TableNameOverride.withTableNameReplacement(tableName))
            .withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES)
            .build();

Json to be inserted is

{"id" : 1,"name":"xyz"}

Now while using DynamoMapper.save(item, dynamoDBMapperConfig) to update the record it is updating "age" attribute to 0 while address attribute is unchanged(which is required too).

This case is with all the java primitives number types like int,float etc and it is fine with wrapper classes like Integer,Float. I dont know, why primitives types are getting overridden to default values. Any suggestion?

1

There are 1 best solutions below

0
On

SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES will only skip null properties. Java Primitives cannot be null, so are initialised with appropriate defaults such as 0 for int types.

'Address' in your case is a String, which is a Java Object, and Objects in Java are initialised with null. Similarly, if you change 'age' in your POJO to the Object type Integer (the Wrapper-Class for Primitive ints), it should not get overwritten.

On another note, it is common practice to use lower camelCase when naming properties in Java (i.e. 'Name' & 'Address' should be 'name' & 'address' respectively). You can use @DynamoDBAttribute(attributeName = "Address"), for example, to map these to the correct DynamoDB Table Attribute names.