DynamoDB
Mapper annotation
Object mapping
Java development
Nested objects

DynamoDB Mapper annotation for Object which has list of another object

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Overview

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. One of the features that makes DynamoDB powerful for developers is the Amazon DynamoDB Mapper (DynamoDBMapper), a Java-based ORM (Object-Relational Mapping) tool to interact easily with DynamoDB tables. In this article, we will focus on using DynamoDB Mapper annotations, particularly for an object containing a list of another object.

DynamoDB Mapper Annotations

DynamoDB Mapper simplifies the interactions between Java objects and DynamoDB's tables by leveraging Java annotations. These annotations enable Java developers to map their classes to DynamoDB tables without writing extensive boilerplate code. Here are some key annotations used with DynamoDB Mapper:

  • @DynamoDBTable: Indicates the specific table in DynamoDB.
  • @DynamoDBHashKey: Specifies the hash key for the table.
  • @DynamoDBRangeKey: Used if the table also requires a range key.
  • @DynamoDBAttribute: Maps a class attribute to a table attribute.

Mapping a List of Objects

When working with DynamoDB Mapper, you may often need to interact with complex data structures such as lists of objects. Suppose you have a table representing a person, and each person can have multiple addresses. Here’s a concise way to represent this relationship using DynamoDB Mapper annotations.

Example

Below is a practical example illustrating how to map a list of Address objects to the Person table.

java
1import com.amazonaws.services.dynamodbv2.datamodeling.*;
2
3import java.util.List;
4
5@DynamoDBTable(tableName = "Person")
6public class Person {
7
8    private String personId;
9    private String name;
10    private List<Address> addresses;
11
12    @DynamoDBHashKey(attributeName = "PersonId")
13    public String getPersonId() { return personId; }
14    public void setPersonId(String personId) { this.personId = personId; }
15
16    @DynamoDBAttribute(attributeName = "Name")
17    public String getName() { return name; }
18    public void setName(String name) { this.name = name; }
19
20    @DynamoDBAttribute(attributeName = "Addresses")
21    public List<Address> getAddresses() { return addresses; }
22    public void setAddresses(List<Address> addresses) { this.addresses = addresses; }
23}
24
25public class Address {
26
27    private String street;
28    private String city;
29    private String zipCode;
30
31    public String getStreet() { return street; }
32    public void setStreet(String street) { this.street = street; }
33
34    public String getCity() { return city; }
35    public void setCity(String city) { this.city = city; }
36
37    public String getZipCode() { return zipCode; }
38    public void setZipCode(String zipCode) { this.zipCode = zipCode; }
39}

Explanation

  • Primary Key: The Person class marks personId with the @DynamoDBHashKey. This ensures that DynamoDB treats personId as the primary key for the Person table.
  • Attributes: The name and addresses fields are marked with @DynamoDBAttribute, indicating that these are table attributes.
  • Complex Data: The Address list is stored as a JSON array by default. DynamoDB stores this list directly as an attribute in the table entry for a person.

Considerations

Using lists of embedded objects in DynamoDB does come with considerations:

  • Limited Querying: You cannot directly query against the inner fields of Address; you have to fetch the whole list and process it client-side.
  • Size Limitations: There is a storage size constraint per item; AWS DynamoDB currently supports a maximum of 400KB per item.

Summary Table

Here's a quick summary of the key concepts regarding DynamoDB Mapper annotations:

AnnotationDescriptionApplicable on
@DynamoDBTableAssociates a class with a DynamoDB tableClass
@DynamoDBHashKeyMarks a primary key fieldField/Getter Method
@DynamoDBRangeKeyMarks a range key fieldField/Getter Method
@DynamoDBAttributeMaps a field to a DynamoDB attributeField/Getter Method
List of ObjectsStores as JSON within a table attributeRequires @DynamoDBAttribute

Additional Tips

  • Custom Converters: If needed, you can create a custom converter by implementing DynamoDBTypeConverter to gain more control over how the data is saved and retrieved.
  • Enhanced Interface: Consider using Amazon’s enhanced DynamoDB document API for handling more complex queries and operations.

By effectively utilizing DynamoDB Mapper annotations, developers can efficiently map Java objects to complex data models in DynamoDB, thereby reducing development time and increasing productivity.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design