Reputation: 33
Is possible to create a relationship between two tables using DynamoDB Java Persistence Model?
I have the follow relationship of One Post to Many Comments
@DynamoDBTable(tableName="Post_MyApp")
public class Post {
private String id;
private String title;
private Set <Comment> comments;
//... Getters and setters and dynamo annotations
}
I have a separated table of comments, that Comment is another dynamo table/entity. My idea is create a table of post_comments like in SQL with all the comments of a post. This is the right way to do this with Dynamo or there is another way to do it better?
Upvotes: 3
Views: 3000
Reputation: 4052
One post to many comments. You just need the following tables: 1. Post table: post-id (Hash Key), 2. Comment table: post-id (Hash Key), comment-id (Range Key)
To get all the comment for a post, you query the comment table by just giving the HashKey (post-id).
On Java side, you will have 2 classes (Post, Comments) mapped with the Dynamo DB Mapper.
DynamoDBMapper (AWS SDK for Java - 1.7.1) : http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodb/datamodeling/DynamoDBMapper.html
Upvotes: 4
Reputation: 7132
I don't know Java SDK at all but, from a design point of view, may I suggest you a "DynamoDBier" approach ?
Post_MyApp
hash_key
Comments_MyApp
hash_key
range_key
This allows you to get all comments for an article in a single Query
and even to paginate using limit
and ExclusiveStartKey
By the way, I found excellent examples In DynamoDb documentation's appendix: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/SampleTablesAndData.html
Upvotes: 0