diff options
-rw-r--r-- | src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java index b0aa615..d522b1a 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java @@ -222,9 +222,6 @@ public abstract class Post<Text extends PostText<?>> extends PanacheEntityBase { cb = cb.where("post.scope").eq(Scope.top_level); - cb = cb.leftJoinFetch("post.comments", "comment"); - cb = cb.fetch("comment.texts"); - return cb; } @@ -373,8 +370,9 @@ public abstract class Post<Text extends PostText<?>> extends PanacheEntityBase { } } - // Fetch texts (to avoid n+1 selects). + // Fetch texts and comments (to avoid n+1 selects). fetchTexts(forwardResults); + fetchComments(forwardResults); return new PostPage<>(prevCursor, cursor, nextCursor, forwardResults); } @@ -388,6 +386,15 @@ public abstract class Post<Text extends PostText<?>> extends PanacheEntityBase { } } + public static <T extends Post<?>> void fetchComments(Collection<T> posts) { + var postIds = posts.stream().map(x -> x.id).collect(toList()); + + if (!postIds.isEmpty()) { + find("SELECT p FROM Post p LEFT JOIN FETCH p.comments WHERE p.id IN (?1)", postIds).stream() + .count(); + } + } + @CheckForNull public Text getText() { var texts = getTexts(); |