Right now I can't get the concept behind Spring Data REST if it comes to complex aggregate roots. If I understand Domain Driven Design correctly (which is AFAIK the base principle for spring data?), you only expose aggregate roots through repositories.
Let's say I have two classes Post
and Comment
. Both are entities and Post has a @OneToMany List<Comment> comments
.
Since Post
is obviously the aggregate root I'd like to access it through a PostRepository
. If I create @RepositoryRestResource public interface PostRepository extends CrudRepository<Post, Long>
REST access to Post
works fine.
Now comments
is renderd inline and is not exposed as a sub resource like /posts/{post}/comments
. This happens only if I introduce a CommentRepository
(which I shouldn't do if I want to stick to DDD).
So how do you use Spring Data REST properly with complex domain objects? Let's say you have to check that all comments does not contain more than X characters alltogether. This would clearly be some invariant handled by the Post
aggregate root. Where would you place the logic for Post.addComment()
? How do you expose other classes as sub resources so I can access /posts/{post}/comments/{comment}
without introducing unnecessary repositories?