Praveen
Praveen

Reputation: 97

DynamoDB with boolean attribute using AWS SDK JAVA

DynamoDBNativeBoolean is deprecated now. Is there any other opt? If you have sample please help out on that. I am using like below but it is stored as 0 or 1, while retrieving the date (scan/query) using withBOOL and it is not returning the result. As it changed to 0 or 1 and the data type to Number.

@DynamoDBAttribute(attributeName = "ischeckin") 
public Boolean getIscheckin() {
    return ischeckedin;
}
public void setIscheckin(Boolean ischeckin) {
    this.ischeckin = ischeckid;
}

Upvotes: 2

Views: 7634

Answers (1)

statut
statut

Reputation: 909

You should use @DynamoDBTyped(DynamoDBAttributeType.BOOL) instead of @DynamoDBNativeBoolean. Please see the example below.

I was using aws-java-sdk-dynamodb:1.11.293

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

and the following Entity

@DynamoDBTable(tableName = "Test")
public static class Entity {

    private String id;

    private boolean value;

    public Entity() {
    }

    public Entity(String id, boolean value) {
        this.id = id;
        this.value = value;
    }

    @DynamoDBHashKey(attributeName = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @DynamoDBTyped(DynamoDBAttributeType.BOOL)
    @DynamoDBAttribute(attributeName = "value")
    public boolean isValue() {
        return value;
    }

    public void setValue(boolean value) {
        this.value = value;
    }
}

and main method

public static void main(String[] args) {
    AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard()
        .withRegion(Regions.US_EAST_1)
        .withCredentials(new AWSStaticCredentialsProvider(
            new BasicAWSCredentials("accessKey", "secretKey")))
        .build();

    DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);

    mapper.save(new Entity("id", true));

    Entity entity = mapper.load(Entity.class, "id");
    System.out.println(entity.getId() + " " + entity.isValue());
}

Upvotes: 5

Related Questions