aws-sdk-java icon indicating copy to clipboard operation
aws-sdk-java copied to clipboard

transactionWrite not working with `UPDATE_SKIP_NULL_ATTRIBUTES`

Open mamakancha opened this issue 5 years ago • 8 comments

Describe the bug

I'm trying to use DynamoDB transactions in the following manner ( using Kotlin ) :

val transactionWriteRequest = TransactionWriteRequest()
transactionWriteRequest.addUpdate(node)
mapper.transactionWrite(transactionWriteRequest, dynamoDBMapperConfig)

Where, dynamoDBMapperConfig is defined as :

dynamoDBMapperConfig = DynamoDBMapperConfig.Builder()
            .withConsistentReads(DynamoDBMapperConfig.ConsistentReads.CONSISTENT)
            .withSaveBehavior(DynamoDBMapperConfig.SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES)
            .build()

My object looks like :


id : 123
name : xyz
createdAt : null

In my DDB Table, this is stored as

id : 123
name : abc 
createdAt : 1636380634908

Expected behavior

The table should've had the following entry :

id : 123
name : xyz
createdAt : 1636380634908

Current behavior

The table rather has :

id : 123
name : abc 

createdAt should've been skipped but was removed instead from the table.

Steps to Reproduce

Create an object and insert it into dynamodb table with the following attributes :

id ( PK ) , name, createdAt

Then update the same object with createdAt set as null.

Using the following dependency :

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-dynamodb</artifactId>
    <version>1.12.70</version>
</dependency>

Possible Solution

generateUpdate method ( In DynamoDBMapper.java ) is not considering the fact that UPDATE_SKIP_NULL_ATTRIBUTES was passed as part of config and hence creating a map of null non key values and removing them.

Context

No response

AWS Java SDK version used

1.12.70

JDK version used

11

Operating System and version

macOS Big Sur, 11.6

mamakancha avatar Nov 08 '21 15:11 mamakancha

Apologies for the delay, taking a look.

debora-ito avatar Nov 24 '21 23:11 debora-ito

I think SaveBehavior is not supported in transactionWrite. I'm digging into the code to confirm because the docs don't explicitly say that.

debora-ito avatar Nov 26 '21 19:11 debora-ito

And, of course, immediately after I wrote that comment I found a documentation (DynamoDBMapper link):

This method ignores any SaveBehavior set on the mapper. Whether an object is put or updated is solely determined by the TransactionWriteRequest method called by user while constructing request object. Furthermore, put and update work as if SaveBehavior is set as CLOBBER.

debora-ito avatar Nov 26 '21 19:11 debora-ito

Changing to a Guidance question and marking this to auto-close soon. Add a comment if you have further questions, it will prevent the issue to be auto-closed.

debora-ito avatar Nov 26 '21 19:11 debora-ito

how to sovle the issue ?i don't want to update all the null fields

weibian-wyze avatar Sep 21 '22 12:09 weibian-wyze

@mamakancha have you solved this issue? could you share me your solution? thanks

weibian-wyze avatar Sep 22 '22 06:09 weibian-wyze

i found a way to work around. just create a DDB mapper POJO class to put the fields that your want to update and the hash fields in it. other field will not be chagned after transactionwrite

weibian-wyze avatar Sep 22 '22 07:09 weibian-wyze

@weibian-wyze can you please explain your approach a bit or better if you could share an approach commit. Thanks!

pmittal21 avatar Mar 04 '24 16:03 pmittal21