Is there a way to fetch the DynamoDB attribute name if we know the attribute name of the entity class?

900 views Asked by At

Given a DynamoDB entity like this

@Data()
@DynamoDBTable(tableName = "DynamoTable")
public class DynamoTable implements Serializable {
    @DynamoDBAttribute(attributeName = "DATA_VALUE")
    private String data

    …
}

What I want to know is if I know the name of the attribute in the entity class (data), could I get the name of the DynamoDB attribute it is annotated with in the entity class (DATA_VALUE)?

1

There are 1 answers

0
madhead On

It can be simply achieved with reflection:

final Field field = DynamoTable.class.getField("data");
final DynamoDBAttribute annotation = field.getAnnotation(DynamoDBAttribute.class);

if(null != annotation) {
    System.out.println(annotation.attributeName());
}