Search code examples
javaandroidamazon-web-servicesamazon-dynamodbgetter-setter

DynamoDB mapper save only one attribute in an item Java


I am getting a item record from my dynamodb table adding an attribute (userratings) and then saving it again like this:

 SongDatabaseMappingAdapter song = mapper.load(SongDatabaseMappingAdapter.class, params[0]);
            song.getUserRatings().add(userID + "-" + params[1] + "-" + sdf.format(date));
            mapper.save(song);

The problem is in my SongDatabaseMappingAdapter I have some extra getter setters that are being used to update the GUI, like this:

public Boolean getOneStarRating() {
        return onestarrating;
    }

    public void setOneStarRating(Boolean onestarrating) {

        this.onestarrating = onestarrating;
    }

I need them to be there but I don't want them to save to the attributes in my database but when I run my code to save the item is adding all the attributes which is every element in my getter/setter (Mapping Adapter) into the item so I end up with extra attributes (columns) like below (All of the star ratings below have been added but shouldn't be there):

UserRatings fiveStarRating fourStarRating oneStarRating

So basically I want to change my code so it is only saving the userratings attribute in the song item not all of the attributes. I was thinking of something like below, but I am not sure how to do it (the code below doesn't work)

  SongDatabaseMappingAdapter song = mapper.load(SongDatabaseMappingAdapter.class, params[0]);
                song.getUserRatings().add(userID + "-" + params[1] + "-" + sdf.format(date));
                mapper.save(song.setUserRatings());

Thanks for your help


Solution

  • Annotate the extra attributes with @DynamoDBIgnore. As per the documentation here:

    DynamoDBIgnore

    Indicates to the DynamoDBMapper instance that the associated property should be ignored. When saving data to the table, the DynamoDBMapper does not save this property to the table.