Reputation: 255
I have 3 entities (User, Post, Comment):
@Entity
public class User {
@Id private Long id;
@OneToMany private Set<Post> posts;
...
}
@Entity
public class Post {
@Id private Long id;
@ManyToOne private User user;
@OneToMany private Set<Comment> comments;
...
}
@Entity
public class Comment {
@Id private Long id;
@ManyToOne private Post post;
...
}
Now I can select only users with posts:
List<User> users = query.from(qUser)
.leftJoin(qUser.posts, qPost)
.transform(groupBy(qUser.id).list(
Projections.bean(User.class, qUser.id, ..., set(
Projections.bean(Post.class, qPost.id, ...)
).as(qUser.posts))
));
But i need to select users with posts and with comments in one query, like:
List<User> users = query.from(qUser)
.leftJoin(qUser.posts, qPost)
.leftJoin(qPost.comments, qComment)
.transform(groupBy(qUser.id).list(
Projections.bean(User.class, qUser.id, ..., set(
Projections.bean(Post.class, qPost.id, ...,
set(Comment.class, qComment.id, ...).as(qPost,comments)
)
).as(qUser.posts))
));
How can I do this?
Upvotes: 0
Views: 848
Reputation: 4724
You need a third projection for your comments.
List<User> users = query.from(qUser)
.leftJoin(qUser.posts, qPost)
.leftJoin(qPost.comments, qComment)
.transform(groupBy(qUser.id).list(
Projections.bean(User.class, qUser.id, ...,
set(Projections.bean(Post.class, qPost.id, ...,
set(Projections.bean(Comment.class, qComment.id, ...).as("comments")
).as("posts")
)
);
Upvotes: 1