JPA aggregate root approach

By | October 30, 2018
Questions:

I’ll use a blog post as an example.

The BlogPost entity is an aggregate root, and it contains a BlogPostComment collection.
That’s good because a post comment cannot exist without a parent blog post. Every operation on a BlogPostComment is taken care of by a BlogPost, or better by the service which manages BlogPost entities.

However, by taking this route, every time I want to add a comment, I’m forced to retrieve all the blog post information. Every time I want to remove a comment, I’m forced to retrieve all the blog post information. Etc.

So the first and most obvious solution would be to offer a separate service to operate on BlogPostComment(s). But that would violate the concept of a post comment. You shouldn’t be able to operate on comments alone.

What’s the right way to do this?

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *