aws / aws-sdk-java

The official AWS SDK for Java 1.x. The AWS SDK for Java 2.x is available here: https://github.com/aws/aws-sdk-java-v2/
https://aws.amazon.com/sdkforjava
Apache License 2.0
4.11k stars 2.83k forks source link

transactionWrite not working with `UPDATE_SKIP_NULL_ATTRIBUTES` #2664

Closed mamakancha closed 2 years ago

mamakancha commented 2 years ago

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

debora-ito commented 2 years ago

Apologies for the delay, taking a look.

debora-ito commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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.

weibian-wyze commented 1 year ago

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

weibian-wyze commented 1 year ago

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

weibian-wyze commented 1 year ago

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

pmittal21 commented 5 months ago

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