DynamoDB storing hash value instead of json

Nacho321 :

I'm using Java 8 and the AWS SDK 1.11 version.

I have the following table definition:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBVersionAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DoNotTouch;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Set;

/**
 * Mapping between a tenant and the roles they defined to access their S3 Attachment buckets.
 */
@DynamoDBTable(tableName = CustomerRoleTenantMapping.TABLE_NAME)
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@DoNotTouch
public class CustomerRoleTenantMapping {

    public static final String TABLE_NAME = "CustomerRoleTenantMapping";

    /**
     * The identifier of this mapping between the tenant and the roles.
     */
    @DynamoDBHashKey(attributeName = "tenant")
    private String tenant;

    /**
     * The name of the tenant.
     */
    @DynamoDBAttribute(attributeName = "name")
    private String name;

    /**
     * The system that a tenant has allowed to access their S3 Attachment buckets.
     */
    @DynamoDBAttribute(attributeName = "tenantPlatform")
    private String tenantPlatform;

    /**
     * The roles associated to a tenant's S3 Attachment buckets.
     */
    @DynamoDBAttribute(attributeName = "customerRoles")
    private Set<RoleArn> customerRoles;

    @DynamoDBAttribute(attributeName = "version")
    @DynamoDBVersionAttribute
    private Long version;
}

The RoleArn class is defined like this:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.encryption.DoNotTouch;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;

import java.util.Objects;

/**
 * Model for AWS Role ARNs.
 */
@DynamoDBDocument
@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@DoNotTouch
public class RoleArn {

    /**
     * The Role Arn.
     */
    @NonNull
    @DynamoDBAttribute(attributeName = "arn")
    private String arn;

    /**
     * The description of the role.
     */
    @NonNull
    @DynamoDBAttribute(attributeName = "description")
    private String description;

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof RoleArn) {
            return ((RoleArn) obj).arn.equals(arn);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(arn);
    }
}

My problem is that when I try to store a CustomerRoleTenantMapping object in my DDB, the RoleArn is stored not as a JSON but as the hash of the instance.

Just FYI, the reason why I had to implement the equals() and hashCode() in the RoleArn is to avoid duplicated RoleArns objects in the same instance of CustomerRoleTenantMapping by comparing just the arn attribute of two instances of RoleArns.

What can I do to store the JSON of the Set instead of the hash?

F_SO_K :

DynamoDB mapper can only convert a limited set of objects by default:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.DataTypes.html

Since you have a set of RoleArn objects you will need to use the @DynamoDBTypeConverted annotation and a bespoke TypeConvertor class

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.ArbitraryDataMapping.html

Update: In short you have to write another class that tells DynamoDBmapper how to turn your complex object into json, and then unconvert the json string back into the complex object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=352838&siteId=1